src/ngx_pagespeed.cc
// Handle responses where we have the content we need in memory and can just
// send it right out.
Handle responses where we have the content we need in memory and can just
send it right out.
Handle responses where we have the content we need in memory and can just send it right out .
ngx_int_t ps_simple_handler(ngx_http_request_t* r,
                            NgxServerContext* server_context,
                            RequestRouting::Response response_category) {
  NgxRewriteDriverFactory* factory =
      static_cast<NgxRewriteDriverFactory*>(
          server_context->factory());
  NgxMessageHandler* message_handler = factory->ngx_message_handler();
  StringPiece request_uri_path = str_to_string_piece(r->uri);
  GoogleString url_string = ps_determine_url(r);
  GoogleUrl url(url_string);
  QueryParams query_params;
  if (url.IsWebValid()) {
    query_params.ParseFromUrl(url);
  }
  GoogleString output;
  StringWriter writer(&output);
  HttpStatus::Code status = HttpStatus::kOK;
  ContentType content_type = kContentTypeHtml;
  StringPiece cache_control = HttpAttributes::kNoCache;
  const char* error_message = NULL;
  switch (response_category) {
    case RequestRouting::kStaticContent: {
      StringPiece file_contents;
      if (!server_context->static_asset_manager()->GetAsset(
              request_uri_path.substr(factory->static_asset_prefix().length()),
              &file_contents, &content_type, &cache_control)) {
        return NGX_DECLINED;
      }
      file_contents.CopyToString(&output);
      break;
    }
    case RequestRouting::kMessages: {
      GoogleString log;
      StringWriter log_writer(&log);
      if (!message_handler->Dump(&log_writer)) {
        writer.Write("Writing to ngx_pagespeed_message failed. \n"
                     "Please check if it's enabled in pagespeed.conf.\n",
                     message_handler);
      } else {
        HtmlKeywords::WritePre(log, "", &writer, message_handler);
      }
      break;
    }
    default:
      ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
                    "ps_simple_handler: unknown RequestRouting.");
      return NGX_ERROR;
  }
  if (error_message != NULL) {
    status = HttpStatus::kNotFound;
    content_type = kContentTypeHtml;
    output = error_message;
  }
  ResponseHeaders response_headers;
  response_headers.SetStatusAndReason(status);
  response_headers.set_major_version(1);
  response_headers.set_minor_version(1);
  response_headers.Add(HttpAttributes::kContentType, content_type.mime_type());
  response_headers.Add("X-Content-Type-Options", "nosniff");
  int64 now_ms = factory->timer()->NowMs();
  response_headers.SetDate(now_ms);
  response_headers.SetLastModified(now_ms);
  response_headers.Add(HttpAttributes::kCacheControl, cache_control);
  char* cache_control_s = string_piece_to_pool_string(r->pool, cache_control);
  if (cache_control_s != NULL) {
    if (FindIgnoreCase(cache_control, "private") == StringPiece::npos) {
      response_headers.Add(HttpAttributes::kEtag, "W/\"0\"");
    }
  }
  return send_out_headers_and_body(r, response_headers, output);
}
ngx_int_t ps_simple_handler ( ngx_http_request_t * r , NgxServerContext * server_context , RequestRouting :: Response response_category ) { NgxRewriteDriverFactory * factory = static_cast < NgxRewriteDriverFactory * > ( server_context -> factory ( ) ) ; NgxMessageHandler * message_handler = factory -> ngx_message_handler ( ) ; StringPiece request_uri_path = str_to_string_piece ( r -> uri ) ; GoogleString url_string = ps_determine_url ( r ) ; GoogleUrl url ( url_string ) ; QueryParams query_params ; if ( url . IsWebValid ( ) ) { query_params . ParseFromUrl ( url ) ; } GoogleString output ; StringWriter writer ( & output ) ; HttpStatus :: Code status = HttpStatus :: kOK ; ContentType content_type = kContentTypeHtml ; StringPiece cache_control = HttpAttributes :: kNoCache ; const char * error_message = NULL ; switch ( response_category ) { case RequestRouting :: kStaticContent : { StringPiece file_contents ; if ( ! server_context -> static_asset_manager ( ) -> GetAsset ( request_uri_path . substr ( factory -> static_asset_prefix ( ) . length ( ) ) , & file_contents , & content_type , & cache_control ) ) { return NGX_DECLINED ; } file_contents . CopyToString ( & output ) ; break ; } case RequestRouting :: kMessages : { GoogleString log ; StringWriter log_writer ( & log ) ; if ( ! message_handler -> Dump ( & log_writer ) ) { writer . Write ( " \n " " \n " , message_handler ) ; } else { HtmlKeywords :: WritePre ( log , " " , & writer , message_handler ) ; } break ; } default : ngx_log_error ( NGX_LOG_WARN , r -> connection -> log , 0 , " " ) ; return NGX_ERROR ; } if ( error_message != NULL ) { status = HttpStatus :: kNotFound ; content_type = kContentTypeHtml ; output = error_message ; } ResponseHeaders response_headers ; response_headers . SetStatusAndReason ( status ) ; response_headers . set_major_version ( 1 ) ; response_headers . set_minor_version ( 1 ) ; response_headers . Add ( HttpAttributes :: kContentType , content_type . mime_type ( ) ) ; response_headers . Add ( " " , " " ) ; int64 now_ms = factory -> timer ( ) -> NowMs ( ) ; response_headers . SetDate ( now_ms ) ; response_headers . SetLastModified ( now_ms ) ; response_headers . Add ( HttpAttributes :: kCacheControl , cache_control ) ; char * cache_control_s = string_piece_to_pool_string ( r -> pool , cache_control ) ; if ( cache_control_s != NULL ) { if ( FindIgnoreCase ( cache_control , " " ) == StringPiece :: npos ) { response_headers . Add ( HttpAttributes :: kEtag , " \" \" " ) ; } } return send_out_headers_and_body ( r , response_headers , output ) ; }
Handle responses where we have the content we need in memory and can just
send it right out.
Handle responses where we have the content we need in memory and can just send it right out .
// http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx // Script and styleSheet elements will reject responses with // incorrect MIME types if the server sends the response header // "X-Content-Type-Options: nosniff". This is a security feature // that helps prevent attacks based on MIME-type confusion.