src/ngx_fetch.cc
// Prepare the request data for this fetch, and hook the write event.
Prepare the request data for this fetch, and hook the write event.
Prepare the request data for this fetch and hook the write event .
int NgxFetch::InitRequest() {
  in_ = ngx_create_temp_buf(pool_, 4096);
  if (in_ == NULL) {
    return NGX_ERROR;
  }
  FixUserAgent();
  RequestHeaders* request_headers = async_fetch_->request_headers();
  ConstStringStarVector v;
  size_t size = 0;
  bool have_host = false;
  GoogleString port;
  response_handler = NgxFetch::HandleStatusLine;
  int rc = Connect();
  if (rc == NGX_AGAIN || rc == NGX_OK) {
    if (connection_->keepalive()) {
      request_headers->Add(HttpAttributes::kConnection,
                           NgxConnection::ka_header);
    }
    const char* method = request_headers->method_string();
    size_t method_len = strlen(method);
    size = (method_len +
            1  +
            url_.uri.len +
            sizeof(" HTTP/1.0\r\n") - 1);
    for (int i = 0; i < request_headers->NumAttributes(); i++) {
      if (StringCaseEqual(request_headers->Name(i), "Host")) {
        have_host = true;
      }
      size += request_headers->Name(i).length()
          + request_headers->Value(i).length() + 4;  
    }
    if (!have_host) {
      port = StrCat(":", IntegerToString(url_.port));
      size += url_.host.len + 8 + port.size();
    }
    size += 2;  
    out_ = ngx_create_temp_buf(pool_, size);
    if (out_ == NULL) {
      return NGX_ERROR;
    }
    out_->last = ngx_cpymem(out_->last, method, method_len);
    out_->last = ngx_cpymem(out_->last, " ", 1);
    out_->last = ngx_cpymem(out_->last, url_.uri.data, url_.uri.len);
    out_->last = ngx_cpymem(out_->last, " HTTP/1.0\r\n", 11);
    if (!have_host) {
      out_->last = ngx_cpymem(out_->last, "Host: ", 6);
      out_->last = ngx_cpymem(out_->last, url_.host.data, url_.host.len);
      out_->last = ngx_cpymem(out_->last, port.c_str(), port.size());
      out_->last = ngx_cpymem(out_->last, "\r\n", 2);
    }
    for (int i = 0; i < request_headers->NumAttributes(); i++) {
      const GoogleString& name = request_headers->Name(i);
      const GoogleString& value = request_headers->Value(i);
      out_->last = ngx_cpymem(out_->last, name.c_str(), name.length());
      *(out_->last++) = ':';
      *(out_->last++) = ' ';
      out_->last = ngx_cpymem(out_->last, value.c_str(), value.length());
      *(out_->last++) = CR;
      *(out_->last++) = LF;
    }
    *(out_->last++) = CR;
    *(out_->last++) = LF;
    if (rc == NGX_AGAIN) {
      return NGX_OK;
    }
  } else if (rc < NGX_OK) {
    return rc;
  }
  CHECK(rc == NGX_OK);
  NgxFetch::ConnectionWriteHandler(connection_->c_->write);
  return NGX_OK;
}
int NgxFetch :: InitRequest ( ) { in_ = ngx_create_temp_buf ( pool_ , 4096 ) ; if ( in_ == NULL ) { return NGX_ERROR ; } FixUserAgent ( ) ; RequestHeaders * request_headers = async_fetch_ -> request_headers ( ) ; ConstStringStarVector v ; size_t size = 0 ; bool have_host = false ; GoogleString port ; response_handler = NgxFetch :: HandleStatusLine ; int rc = Connect ( ) ; if ( rc == NGX_AGAIN || rc == NGX_OK ) { if ( connection_ -> keepalive ( ) ) { request_headers -> Add ( HttpAttributes :: kConnection , NgxConnection :: ka_header ) ; } const char * method = request_headers -> method_string ( ) ; size_t method_len = strlen ( method ) ; size = ( method_len + 1 + url_ . uri . len + sizeof ( " \r \n " ) - 1 ) ; for ( int i = 0 ; i < request_headers -> NumAttributes ( ) ; i ++ ) { if ( StringCaseEqual ( request_headers -> Name ( i ) , " " ) ) { have_host = true ; } size += request_headers -> Name ( i ) . length ( ) + request_headers -> Value ( i ) . length ( ) + 4 ; } if ( ! have_host ) { port = StrCat ( " " , IntegerToString ( url_ . port ) ) ; size += url_ . host . len + 8 + port . size ( ) ; } size += 2 ; out_ = ngx_create_temp_buf ( pool_ , size ) ; if ( out_ == NULL ) { return NGX_ERROR ; } out_ -> last = ngx_cpymem ( out_ -> last , method , method_len ) ; out_ -> last = ngx_cpymem ( out_ -> last , " " , 1 ) ; out_ -> last = ngx_cpymem ( out_ -> last , url_ . uri . data , url_ . uri . len ) ; out_ -> last = ngx_cpymem ( out_ -> last , " \r \n " , 11 ) ; if ( ! have_host ) { out_ -> last = ngx_cpymem ( out_ -> last , " " , 6 ) ; out_ -> last = ngx_cpymem ( out_ -> last , url_ . host . data , url_ . host . len ) ; out_ -> last = ngx_cpymem ( out_ -> last , port . c_str ( ) , port . size ( ) ) ; out_ -> last = ngx_cpymem ( out_ -> last , " \r \n " , 2 ) ; } for ( int i = 0 ; i < request_headers -> NumAttributes ( ) ; i ++ ) { const GoogleString & name = request_headers -> Name ( i ) ; const GoogleString & value = request_headers -> Value ( i ) ; out_ -> last = ngx_cpymem ( out_ -> last , name . c_str ( ) , name . length ( ) ) ; * ( out_ -> last ++ ) = ' ' ; * ( out_ -> last ++ ) = ' ' ; out_ -> last = ngx_cpymem ( out_ -> last , value . c_str ( ) , value . length ( ) ) ; * ( out_ -> last ++ ) = CR ; * ( out_ -> last ++ ) = LF ; } * ( out_ -> last ++ ) = CR ; * ( out_ -> last ++ ) = LF ; if ( rc == NGX_AGAIN ) { return NGX_OK ; } } else if ( rc < NGX_OK ) { return rc ; } CHECK ( rc == NGX_OK ) ; NgxFetch :: ConnectionWriteHandler ( connection_ -> c_ -> write ) ; return NGX_OK ; }
Prepare the request data for this fetch, and hook the write event.
Prepare the request data for this fetch and hook the write event .
/* for the space */ // if no explicit host header is given in the request headers, // we need to derive it from the url. // name: value\r\n // 4 for ": \r\n" // for "Host: " + host + ":" + port + "\r\n" // "\r\n";