src/ngx_pagespeed.cc
// Check whether this visitor is already in an experiment.  If they're not,
// classify them into one by setting a cookie.  Then set options appropriately
// for their experiment.
//
// See InstawebContext::SetExperimentStateAndCookie()
Check whether this visitor is already in an experiment.  If they're not,
classify them into one by setting a cookie.  Then set options appropriately
for their experiment.


Check whether this visitor is already in an experiment . If they ' re not classify them into one by setting a cookie . Then set options appropriately for their experiment .
bool ps_set_experiment_state_and_cookie(ngx_http_request_t* r,
                                        RequestHeaders* request_headers,
                                        RewriteOptions* options,
                                        const StringPiece& host) {
  CHECK(options->running_experiment());
  ps_srv_conf_t* cfg_s = ps_get_srv_config(r);
  bool need_cookie =
      cfg_s->server_context->experiment_matcher()->ClassifyIntoExperiment(
          *request_headers, *cfg_s->server_context->user_agent_matcher(),
          options);
  if (need_cookie && host.length() > 0) {
    PosixTimer timer;
    int64 time_now_ms = timer.NowMs();
    int64 expiration_time_ms = (time_now_ms +
                                options->experiment_cookie_duration_ms());
    int state = options->experiment_id();
    GoogleString expires;
    ConvertTimeToString(expiration_time_ms, &expires);
    GoogleString value = StringPrintf(
        "%s=%s; Expires=%s; Domain=.%s; Path=/",
        experiment::kExperimentCookie,
        experiment::ExperimentStateToCookieString(state).c_str(),
        expires.c_str(), host.as_string().c_str());
    ngx_table_elt_t* cookie = static_cast<ngx_table_elt_t*>(
        ngx_list_push(&r->headers_out.headers));
    if (cookie == NULL) {
      return false;
    }
    cookie->hash = 1;  
    ngx_str_set(&cookie->key, "Set-Cookie");
    cookie->value.data = reinterpret_cast<u_char*>(
        string_piece_to_pool_string(r->pool, value));
    cookie->value.len = value.size();
  }
  return true;
}
bool ps_set_experiment_state_and_cookie ( ngx_http_request_t * r , RequestHeaders * request_headers , RewriteOptions * options , const StringPiece & host ) { CHECK ( options -> running_experiment ( ) ) ; ps_srv_conf_t * cfg_s = ps_get_srv_config ( r ) ; bool need_cookie = cfg_s -> server_context -> experiment_matcher ( ) -> ClassifyIntoExperiment ( * request_headers , * cfg_s -> server_context -> user_agent_matcher ( ) , options ) ; if ( need_cookie && host . length ( ) > 0 ) { PosixTimer timer ; int64 time_now_ms = timer . NowMs ( ) ; int64 expiration_time_ms = ( time_now_ms + options -> experiment_cookie_duration_ms ( ) ) ; int state = options -> experiment_id ( ) ; GoogleString expires ; ConvertTimeToString ( expiration_time_ms , & expires ) ; GoogleString value = StringPrintf ( " " , experiment :: kExperimentCookie , experiment :: ExperimentStateToCookieString ( state ) . c_str ( ) , expires . c_str ( ) , host . as_string ( ) . c_str ( ) ) ; ngx_table_elt_t * cookie = static_cast < ngx_table_elt_t * > ( ngx_list_push ( & r -> headers_out . headers ) ) ; if ( cookie == NULL ) { return false ; } cookie -> hash = 1 ; ngx_str_set ( & cookie -> key , " " ) ; cookie -> value . data = reinterpret_cast < u_char * > ( string_piece_to_pool_string ( r -> pool , value ) ) ; cookie -> value . len = value . size ( ) ; } return true ; }
Check whether this visitor is already in an experiment.
Check whether this visitor is already in an experiment .
// TODO(jefftk): refactor SetExperimentCookie to expose the value we want to // set on the cookie. // Set the PagespeedExperiment cookie. // Include this header in the response. // It's not safe to use value.c_str here because cookie header only keeps a // pointer to the string data.