Merge branch 'master' into trunk-tracking

This commit is contained in:
Jeff Kaufman
2013-08-27 13:50:47 -04:00
5 changed files with 105 additions and 128 deletions
-10
View File
@@ -42,7 +42,6 @@ NgxBaseFetch::NgxBaseFetch(ngx_http_request_t* r, int pipe_fd,
handle_error_(true),
modify_caching_headers_(modify_caching_headers) {
if (pthread_mutex_init(&mutex_, NULL)) CHECK(0);
PopulateRequestHeaders();
}
NgxBaseFetch::~NgxBaseFetch() {
@@ -57,15 +56,6 @@ void NgxBaseFetch::Unlock() {
pthread_mutex_unlock(&mutex_);
}
void NgxBaseFetch::PopulateRequestHeaders() {
ngx_psol::copy_request_headers_from_ngx(request_, request_headers());
}
void NgxBaseFetch::PopulateResponseHeaders() {
ngx_psol::copy_response_headers_from_ngx(request_, response_headers());
}
bool NgxBaseFetch::HandleWrite(const StringPiece& sp,
MessageHandler* handler) {
Lock();
-6
View File
@@ -61,12 +61,6 @@ class NgxBaseFetch : public AsyncFetch {
bool modify_caching_headers);
virtual ~NgxBaseFetch();
// Copies the request headers out of request_->headers_in->headers.
void PopulateRequestHeaders();
// Copies the response headers out of request_->headers_out->headers.
void PopulateResponseHeaders();
// Puts a chain in link_ptr if we have any output data buffered. Returns
// NGX_OK on success, NGX_ERROR on errors. If there's no data to send, sends
// data only if Done() has been called. Indicates the end of output by
+101 -105
View File
@@ -16,18 +16,16 @@
// Author: x.dinic@gmail.com (Junmin Xiong)
//
// The fetch is started by the main thread. It will fetch the remote resource
// from the specific url asynchronously.
// The fetch is started by the main thread. It will fetch the remote resource
// from the specific url asynchronously.
#ifndef NET_INSTAWEB_NGX_FETCHER_H_
#define NET_INSTAWEB_NGX_FETCHER_H_
#ifndef NET_INSTAWEB_NGX_FETCH_H_
#define NET_INSTAWEB_NGX_FETCH_H_
extern "C" {
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef bool (*response_handler_pt)(ngx_connection_t* c);
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
}
#include "ngx_url_async_fetcher.h"
@@ -40,112 +38,110 @@ extern "C" {
#include "net/instaweb/http/public/response_headers_parser.h"
namespace net_instaweb {
class NgxUrlAsyncFetcher;
class NgxFetch : public PoolElement<NgxFetch> {
public:
NgxFetch(const GoogleString& url,
AsyncFetch* async_fetch,
MessageHandler* message_handler,
ngx_msec_t timeout_ms,
ngx_log_t* log);
~NgxFetch();
// Start the fetch
bool Start(NgxUrlAsyncFetcher* fetcher);
// Show the completed url, for logging purpose
const char* str_url();
// This fetch task is done. Call the done of async_fetch.
// It will copy the buffer to cache.
void CallbackDone(bool success);
typedef bool (*response_handler_pt)(ngx_connection_t* c);
// Show the bytes received
size_t bytes_received();
void bytes_received_add(int64 x);
int64 fetch_start_ms();
void set_fetch_start_ms(int64 start_ms);
int64 fetch_end_ms();
void set_fetch_end_ms(int64 end_ms);
MessageHandler* message_handler();
int get_major_version() {
return static_cast<int>(status_->http_version / 1000);
}
class NgxUrlAsyncFetcher;
class NgxFetch : public PoolElement<NgxFetch> {
public:
NgxFetch(const GoogleString& url,
AsyncFetch* async_fetch,
MessageHandler* message_handler,
ngx_msec_t timeout_ms,
ngx_log_t* log);
~NgxFetch();
int get_minor_version() {
return static_cast<int>(status_->http_version % 1000);
}
// Start the fetch.
bool Start(NgxUrlAsyncFetcher* fetcher);
// Show the completed url, for logging purposes.
const char* str_url();
// This fetch task is done. Call Done() on the async_fetch. It will copy the
// buffer to cache.
void CallbackDone(bool success);
int get_status_code() {
return static_cast<int>(status_->code);
}
// Show the bytes received.
size_t bytes_received();
void bytes_received_add(int64 x);
int64 fetch_start_ms();
void set_fetch_start_ms(int64 start_ms);
int64 fetch_end_ms();
void set_fetch_end_ms(int64 end_ms);
MessageHandler* message_handler();
ngx_event_t* timeout_event() {
return timeout_event_;
};
void set_timeout_event(ngx_event_t* x) {
timeout_event_ = x;
};
int get_major_version() {
return static_cast<int>(status_->http_version / 1000);
}
int get_minor_version() {
return static_cast<int>(status_->http_version % 1000);
}
int get_status_code() {
return static_cast<int>(status_->code);
}
ngx_event_t* timeout_event() {
return timeout_event_;
}
void set_timeout_event(ngx_event_t* x) {
timeout_event_ = x;
}
private:
response_handler_pt response_handler;
// Do the initialized work and start the resolver work.
bool Init();
bool ParseUrl();
// Prepare the request and write it to remote server.
int InitRequest();
// Create the connection with remote server.
int Connect();
void set_response_handler(response_handler_pt handler) {
response_handler = handler;
}
// Only the Static functions could be used in callbacks.
static void NgxFetchResolveDone(ngx_resolver_ctx_t* ctx);
private:
response_handler_pt response_handler;
// Do the initialized work and start the resolver work.
bool Init();
bool ParseUrl();
// Prepare the request and write it to remote server.
int InitRequest();
// Create the connection with remote server.
int Connect();
void set_response_handler(response_handler_pt handler) {
response_handler = handler;
}
// Only the Static functions could be used in callbacks.
static void NgxFetchResolveDone(ngx_resolver_ctx_t* ctx);
// Write the request.
static void NgxFetchWrite(ngx_event_t* wev);
// Wait for the response.
static void NgxFetchRead(ngx_event_t* rev);
// Read and parse the first status line.
static bool NgxFetchHandleStatusLine(ngx_connection_t* c);
// Read and parse the HTTP headers.
static bool NgxFetchHandleHeader(ngx_connection_t* c);
// Read the response body.
static bool NgxFetchHandleBody(ngx_connection_t* c);
// Cancel the fetch when it's timeout.
static void NgxFetchTimeout(ngx_event_t* tev);
// Write the request
static void NgxFetchWrite(ngx_event_t* wev);
// Add the pagespeed User-Agent.
void FixUserAgent();
void FixHost();
// Wait for the response
static void NgxFetchRead(ngx_event_t* rev);
// Read and parse the first status line
static bool NgxFetchHandleStatusLine(ngx_connection_t* c);
// Read and parse the HTTP headers
static bool NgxFetchHandleHeader(ngx_connection_t* c);
// Read the response body
static bool NgxFetchHandleBody(ngx_connection_t* c);
const GoogleString str_url_;
ngx_url_t url_;
NgxUrlAsyncFetcher* fetcher_;
AsyncFetch* async_fetch_;
ResponseHeadersParser parser_;
MessageHandler* message_handler_;
size_t bytes_received_;
int64 fetch_start_ms_;
int64 fetch_end_ms_;
int64 timeout_ms_;
bool done_;
int64 content_length_;
// Cancel the fetch when it's timeout
static void NgxFetchTimeout(ngx_event_t* tev);
struct sockaddr_in sin_;
ngx_log_t* log_;
ngx_buf_t* out_;
ngx_buf_t* in_;
ngx_pool_t* pool_;
ngx_http_request_t* r_;
ngx_http_status_t* status_;
ngx_event_t* timeout_event_;
ngx_connection_t* connection_;
ngx_resolver_ctx_t* resolver_ctx_;
// Add the pagespeed User-Agent
void FixUserAgent();
void FixHost();
const GoogleString str_url_;
ngx_url_t url_;
NgxUrlAsyncFetcher* fetcher_;
AsyncFetch* async_fetch_;
ResponseHeadersParser parser_;
MessageHandler* message_handler_;
size_t bytes_received_;
int64 fetch_start_ms_;
int64 fetch_end_ms_;
int64 timeout_ms_;
bool done_;
int64 content_length_;
struct sockaddr_in sin_;
ngx_log_t* log_;
ngx_buf_t* out_;
ngx_buf_t* in_;
ngx_pool_t* pool_;
ngx_http_request_t* r_;
ngx_http_status_t* status_;
ngx_event_t* timeout_event_;
ngx_connection_t* connection_;
ngx_resolver_ctx_t* resolver_ctx_;
DISALLOW_COPY_AND_ASSIGN(NgxFetch);
};
DISALLOW_COPY_AND_ASSIGN(NgxFetch);
};
} // namespace net_instaweb
#endif // NET_INSTAWEB_NGX_FETCHER_H_
#endif // NET_INSTAWEB_NGX_FETCH_H_
+1 -1
View File
@@ -33,7 +33,7 @@ namespace net_instaweb {
class NgxListIterator {
public:
NgxListIterator(ngx_list_part_t* part);
explicit NgxListIterator(ngx_list_part_t* part);
// Return the next element of the list if there is one, NULL otherwise.
ngx_table_elt_t* Next();
+3 -6
View File
@@ -223,9 +223,8 @@ void copy_headers_from_table(const ngx_list_t &from, Headers* to) {
}
} // namespace
// modify from NgxBaseFetch::PopulateResponseHeaders()
void copy_response_headers_from_ngx(const ngx_http_request_t* r,
net_instaweb::ResponseHeaders* headers) {
net_instaweb::ResponseHeaders* headers) {
headers->set_major_version(r->http_version / 1000);
headers->set_minor_version(r->http_version % 1000);
copy_headers_from_table(r->headers_out.headers, headers);
@@ -235,13 +234,12 @@ void copy_response_headers_from_ngx(const ngx_http_request_t* r,
// Manually copy over the content type because it's not included in
// request_->headers_out.headers.
headers->Add(net_instaweb::HttpAttributes::kContentType,
ngx_psol::str_to_string_piece(r->headers_out.content_type));
ngx_psol::str_to_string_piece(r->headers_out.content_type));
// TODO(oschaaf): ComputeCaching should be called in setupforhtml()?
headers->ComputeCaching();
}
// modify from NgxBaseFetch::PopulateRequestHeaders()
void copy_request_headers_from_ngx(const ngx_http_request_t* r,
net_instaweb::RequestHeaders* headers) {
// TODO(chaizhenhua): only allow RewriteDriver::kPassThroughRequestAttributes?
@@ -2159,9 +2157,8 @@ ngx_int_t ps_html_rewrite_header_filter(ngx_http_request_t* r) {
ps_strip_html_headers(r);
// TODO(jefftk): is this thread safe?
ctx->base_fetch->PopulateResponseHeaders();
copy_response_headers_from_ngx(r, ctx->base_fetch->response_headers());
ps_set_buffered(r, true);
r->filter_need_in_memory = 1;