src/ngx_fetch.cc
// Issue a request after the resolver is done
Issue a request after the resolver is done
Issue a request after the resolver is done
void NgxFetch::ResolveDoneHandler(ngx_resolver_ctx_t* resolver_ctx) {
  NgxFetch* fetch = static_cast<NgxFetch*>(resolver_ctx->data);
  NgxUrlAsyncFetcher* fetcher = fetch->fetcher_;
  if (resolver_ctx->state != NGX_OK) {
    if (fetch->timeout_event() != NULL && fetch->timeout_event()->timer_set) {
      ngx_del_timer(fetch->timeout_event());
      fetch->set_timeout_event(NULL);
    }
    fetch->message_handler()->Message(
        kWarning, "NgxFetch %p: failed to resolve host [%.*s]", fetch,
        static_cast<int>(resolver_ctx->name.len), resolver_ctx->name.data);
    fetch->CallbackDone(false);
    return;
  }
  ngx_uint_t i;
  for (i = 0; i < resolver_ctx->naddrs; i++) {
    ngx_addr_t* ngx_addrs = reinterpret_cast<ngx_addr_t*>(resolver_ctx->addrs);
    if (typeid(*ngx_addrs) == typeid(*resolver_ctx->addrs)) {
      if (reinterpret_cast<struct sockaddr_in*>(ngx_addrs[i].sockaddr)
              ->sin_family == AF_INET) {
        break;
      }
    } else {
      break;
    }
  }
  if (i == resolver_ctx->naddrs) {
    if (fetch->timeout_event() != NULL && fetch->timeout_event()->timer_set) {
      ngx_del_timer(fetch->timeout_event());
      fetch->set_timeout_event(NULL);
    }
    fetch->message_handler()->Message(
        kWarning, "NgxFetch %p: no suitable address for host [%.*s]", fetch,
        static_cast<int>(resolver_ctx->name.len), resolver_ctx->name.data);
    fetch->CallbackDone(false);
  }
  ngx_memzero(&fetch->sin_, sizeof(fetch->sin_));
#if (nginx_version < 1005008)
  fetch->sin_.sin_addr.s_addr = resolver_ctx->addrs[i];
#else
  struct sockaddr_in* sin;
  sin = reinterpret_cast<struct sockaddr_in*>(
      resolver_ctx->addrs[i].sockaddr);
  fetch->sin_.sin_family = sin->sin_family;
  fetch->sin_.sin_addr.s_addr = sin->sin_addr.s_addr;
#endif
  fetch->sin_.sin_family = AF_INET;
  fetch->sin_.sin_port = htons(fetch->url_.port);
  if (0 != fetcher->proxy_.url.len) {
    fetch->sin_.sin_port = htons(fetcher->proxy_.port);
  }
  char* ip_address = inet_ntoa(fetch->sin_.sin_addr);
  ngx_log_error(NGX_LOG_DEBUG, fetch->log_, 0,
                "NgxFetch %p: Resolved host [%V] to [%s]", fetch,
                &resolver_ctx->name, ip_address);
  fetch->release_resolver();
  if (fetch->InitRequest() != NGX_OK) {
    fetch->message_handler()->Message(kError, "NgxFetch: InitRequest failed");
    fetch->CallbackDone(false);
  }
}
void NgxFetch :: ResolveDoneHandler ( ngx_resolver_ctx_t * resolver_ctx ) { NgxFetch * fetch = static_cast < NgxFetch * > ( resolver_ctx -> data ) ; NgxUrlAsyncFetcher * fetcher = fetch -> fetcher_ ; if ( resolver_ctx -> state != NGX_OK ) { if ( fetch -> timeout_event ( ) != NULL && fetch -> timeout_event ( ) -> timer_set ) { ngx_del_timer ( fetch -> timeout_event ( ) ) ; fetch -> set_timeout_event ( NULL ) ; } fetch -> message_handler ( ) -> Message ( kWarning , " " , fetch , static_cast < int > ( resolver_ctx -> name . len ) , resolver_ctx -> name . data ) ; fetch -> CallbackDone ( false ) ; return ; } ngx_uint_t i ; for ( i = 0 ; i < resolver_ctx -> naddrs ; i ++ ) { ngx_addr_t * ngx_addrs = reinterpret_cast < ngx_addr_t * > ( resolver_ctx -> addrs ) ; if ( typeid ( * ngx_addrs ) == typeid ( * resolver_ctx -> addrs ) ) { if ( reinterpret_cast < struct sockaddr_in * > ( ngx_addrs [ i ] . sockaddr ) -> sin_family == AF_INET ) { break ; } } else { break ; } } if ( i == resolver_ctx -> naddrs ) { if ( fetch -> timeout_event ( ) != NULL && fetch -> timeout_event ( ) -> timer_set ) { ngx_del_timer ( fetch -> timeout_event ( ) ) ; fetch -> set_timeout_event ( NULL ) ; } fetch -> message_handler ( ) -> Message ( kWarning , " " , fetch , static_cast < int > ( resolver_ctx -> name . len ) , resolver_ctx -> name . data ) ; fetch -> CallbackDone ( false ) ; } ngx_memzero ( & fetch -> sin_ , sizeof ( fetch -> sin_ ) ) ; #if ( nginx_version < 1005008 ) fetch -> sin_ . sin_addr . s_addr = resolver_ctx -> addrs [ i ] ; #else struct sockaddr_in * sin ; sin = reinterpret_cast < struct sockaddr_in * > ( resolver_ctx -> addrs [ i ] . sockaddr ) ; fetch -> sin_ . sin_family = sin -> sin_family ; fetch -> sin_ . sin_addr . s_addr = sin -> sin_addr . s_addr ; #endif fetch -> sin_ . sin_family = AF_INET ; fetch -> sin_ . sin_port = htons ( fetch -> url_ . port ) ; if ( 0 != fetcher -> proxy_ . url . len ) { fetch -> sin_ . sin_port = htons ( fetcher -> proxy_ . port ) ; } char * ip_address = inet_ntoa ( fetch -> sin_ . sin_addr ) ; ngx_log_error ( NGX_LOG_DEBUG , fetch -> log_ , 0 , " " , fetch , & resolver_ctx -> name , ip_address ) ; fetch -> release_resolver ( ) ; if ( fetch -> InitRequest ( ) != NGX_OK ) { fetch -> message_handler ( ) -> Message ( kError , " " ) ; fetch -> CallbackDone ( false ) ; } }
Issue a request after the resolver is done
Issue a request after the resolver is done
// Find the first ipv4 address. We don't support ipv6 yet. // Old versions of nginx and tengine have a different definition of addrs, // work around to make sure we are using the right type (ngx_addr_t*). // We're using an old version that uses in_addr_t* for addrs. // If no suitable ipv4 address was found, we fail. // Maybe we have Proxy