src/ngx_fetch.cc
// Create the pool, parse the url, add the timeout event and
// hook the DNS resolver if needed. Else we connect directly.
// When this returns false, our caller (NgxUrlAsyncFetcher::StartFetch)
// will call fetch->CallbackDone()
Create the pool, parse the url, add the timeout event and
hook the DNS resolver if needed. Else we connect directly.
When this returns false, our caller (NgxUrlAsyncFetcher::StartFetch)
will call fetch->CallbackDone()
Create the pool parse the url add the timeout event and hook the DNS resolver if needed . Else we connect directly . When this returns false our caller ( NgxUrlAsyncFetcher :: StartFetch ) will call fetch - > CallbackDone ()
bool NgxFetch::Init() {
  pool_ = ngx_create_pool(12288, log_);
  if (pool_ == NULL) {
    message_handler_->Message(kError, "NgxFetch: ngx_create_pool failed");
    return false;
  }
  if (!ParseUrl()) {
    message_handler_->Message(kError, 
                              "NgxFetch: ParseUrl() failed for [%s]:%s",
                              str_url_.c_str(), url_.err);
    return false;
  }
  timeout_event_ = static_cast<ngx_event_t*>(
      ngx_pcalloc(pool_, sizeof(ngx_event_t)));
  if (timeout_event_ == NULL) {
    message_handler_->Message(kError,
                              "NgxFetch: ngx_pcalloc failed for timeout");
    return false;
  }
  timeout_event_->data = this;
  timeout_event_->handler = NgxFetch::TimeoutHandler;
  timeout_event_->log = log_;
  ngx_add_timer(timeout_event_, fetcher_->fetch_timeout_);
  r_ = static_cast<ngx_http_request_t*>(
      ngx_pcalloc(pool_, sizeof(ngx_http_request_t)));
  if (r_ == NULL) {
    message_handler_->Message(kError,
                              "NgxFetch: ngx_pcalloc failed for timer");
    return false;
  }
  status_ = static_cast<ngx_http_status_t*>(
      ngx_pcalloc(pool_, sizeof(ngx_http_status_t)));
  if (status_ == NULL) {
    message_handler_->Message(kError,
                              "NgxFetch: ngx_pcalloc failed for status");
    return false;
  }
  ngx_url_t* tmp_url = &url_;
  if (fetcher_->proxy_.url.len != 0) {
    tmp_url = &fetcher_->proxy_;
  }
  GoogleString s_ipaddress(reinterpret_cast<char*>(tmp_url->host.data),
                           tmp_url->host.len);
  ngx_memzero(&sin_, sizeof(sin_));
  sin_.sin_family = AF_INET;
  sin_.sin_port = htons(tmp_url->port);
  sin_.sin_addr.s_addr = inet_addr(s_ipaddress.c_str());
  if (sin_.sin_addr.s_addr == INADDR_NONE) {
    ngx_resolver_ctx_t temp;
    temp.name.data = tmp_url->host.data;
    temp.name.len = tmp_url->host.len;
    resolver_ctx_ = ngx_resolve_start(fetcher_->resolver_, &temp);
    if (resolver_ctx_ == NULL || resolver_ctx_ == NGX_NO_RESOLVER) {
      message_handler_->Message(
          kError, "NgxFetch: Couldn't start resolving, "
          "is there a proper resolver configured in nginx.conf?");
      return false;
    } else {
      ngx_log_error(NGX_LOG_DEBUG, log_, 0,
                    "NgxFetch %p: start resolve for: %s",
                    this, s_ipaddress.c_str());
    }
    resolver_ctx_->data = this;
    resolver_ctx_->name.data = tmp_url->host.data;
    resolver_ctx_->name.len = tmp_url->host.len;
#if (nginx_version < 1005008)
    resolver_ctx_->type = NGX_RESOLVE_A;
#endif
    resolver_ctx_->handler = NgxFetch::ResolveDoneHandler;
    resolver_ctx_->timeout = fetcher_->resolver_timeout_;
    if (ngx_resolve_name(resolver_ctx_) != NGX_OK) {
      message_handler_->Message(kWarning,
                                "NgxFetch: ngx_resolve_name failed");
      return false;
    }
  } else {
    if (InitRequest() != NGX_OK) {
      message_handler()->Message(kError, "NgxFetch: InitRequest failed");
      return false;
    }
  }
  return true;
}
bool NgxFetch :: Init ( ) { pool_ = ngx_create_pool ( 12288 , log_ ) ; if ( pool_ == NULL ) { message_handler_ -> Message ( kError , " " ) ; return false ; } if ( ! ParseUrl ( ) ) { message_handler_ -> Message ( kError , " " , str_url_ . c_str ( ) , url_ . err ) ; return false ; } timeout_event_ = static_cast < ngx_event_t * > ( ngx_pcalloc ( pool_ , sizeof ( ngx_event_t ) ) ) ; if ( timeout_event_ == NULL ) { message_handler_ -> Message ( kError , " " ) ; return false ; } timeout_event_ -> data = this ; timeout_event_ -> handler = NgxFetch :: TimeoutHandler ; timeout_event_ -> log = log_ ; ngx_add_timer ( timeout_event_ , fetcher_ -> fetch_timeout_ ) ; r_ = static_cast < ngx_http_request_t * > ( ngx_pcalloc ( pool_ , sizeof ( ngx_http_request_t ) ) ) ; if ( r_ == NULL ) { message_handler_ -> Message ( kError , " " ) ; return false ; } status_ = static_cast < ngx_http_status_t * > ( ngx_pcalloc ( pool_ , sizeof ( ngx_http_status_t ) ) ) ; if ( status_ == NULL ) { message_handler_ -> Message ( kError , " " ) ; return false ; } ngx_url_t * tmp_url = & url_ ; if ( fetcher_ -> proxy_ . url . len != 0 ) { tmp_url = & fetcher_ -> proxy_ ; } GoogleString s_ipaddress ( reinterpret_cast < char * > ( tmp_url -> host . data ) , tmp_url -> host . len ) ; ngx_memzero ( & sin_ , sizeof ( sin_ ) ) ; sin_ . sin_family = AF_INET ; sin_ . sin_port = htons ( tmp_url -> port ) ; sin_ . sin_addr . s_addr = inet_addr ( s_ipaddress . c_str ( ) ) ; if ( sin_ . sin_addr . s_addr == INADDR_NONE ) { ngx_resolver_ctx_t temp ; temp . name . data = tmp_url -> host . data ; temp . name . len = tmp_url -> host . len ; resolver_ctx_ = ngx_resolve_start ( fetcher_ -> resolver_ , & temp ) ; if ( resolver_ctx_ == NULL || resolver_ctx_ == NGX_NO_RESOLVER ) { message_handler_ -> Message ( kError , " " " " ) ; return false ; } else { ngx_log_error ( NGX_LOG_DEBUG , log_ , 0 , " " , this , s_ipaddress . c_str ( ) ) ; } resolver_ctx_ -> data = this ; resolver_ctx_ -> name . data = tmp_url -> host . data ; resolver_ctx_ -> name . len = tmp_url -> host . len ; #if ( nginx_version < 1005008 ) resolver_ctx_ -> type = NGX_RESOLVE_A ; #endif resolver_ctx_ -> handler = NgxFetch :: ResolveDoneHandler ; resolver_ctx_ -> timeout = fetcher_ -> resolver_timeout_ ; if ( ngx_resolve_name ( resolver_ctx_ ) != NGX_OK ) { message_handler_ -> Message ( kWarning , " " ) ; return false ; } } else { if ( InitRequest ( ) != NGX_OK ) { message_handler ( ) -> Message ( kError , " " ) ; return false ; } } return true ; }
Create the pool, parse the url, add the timeout event and
hook the DNS resolver if needed.
Create the pool parse the url add the timeout event and hook the DNS resolver if needed .
// The host is either a domain name or an IP address.  First check // if it's a valid IP address and only if that fails fall back to // using the DNS resolver. // Maybe we have a Proxy. // inet_addr returned INADDR_NONE, which means the hostname // isn't a valid IP address.  Check DNS. // TODO(oschaaf): this spams the log, but is useful in the fetcher's // current state