src/http/modules/ngx_http_userid_filter_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. #define NGX_HTTP_USERID_OFF   0
  9. #define NGX_HTTP_USERID_LOG   1
  10. #define NGX_HTTP_USERID_V1    2
  11. #define NGX_HTTP_USERID_ON    3

  12. /* 31 Dec 2037 23:55:55 GMT */
  13. #define NGX_HTTP_USERID_MAX_EXPIRES  2145916555


  14. typedef struct {
  15.     ngx_uint_t  enable;

  16.     ngx_int_t   service;

  17.     ngx_str_t   name;
  18.     ngx_str_t   domain;
  19.     ngx_str_t   path;
  20.     ngx_str_t   p3p;

  21.     time_t      expires;

  22.     u_char      mark;
  23. } ngx_http_userid_conf_t;


  24. typedef struct {
  25.     uint32_t    uid_got[4];
  26.     uint32_t    uid_set[4];
  27.     ngx_str_t   cookie;
  28.     ngx_uint_t  reset;
  29. } ngx_http_userid_ctx_t;


  30. static ngx_http_userid_ctx_t *ngx_http_userid_get_uid(ngx_http_request_t *r,
  31.     ngx_http_userid_conf_t *conf);
  32. static ngx_int_t ngx_http_userid_variable(ngx_http_request_t *r,
  33.     ngx_http_variable_value_t *v, ngx_str_t *name, uint32_t *uid);
  34. static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r,
  35.     ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);
  36. static ngx_int_t ngx_http_userid_create_uid(ngx_http_request_t *r,
  37.     ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);

  38. static ngx_int_t ngx_http_userid_add_variables(ngx_conf_t *cf);
  39. static ngx_int_t ngx_http_userid_init(ngx_conf_t *cf);
  40. static void *ngx_http_userid_create_conf(ngx_conf_t *cf);
  41. static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent,
  42.     void *child);
  43. static char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data);
  44. static char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data);
  45. static char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd,
  46.     void *conf);
  47. static char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data);
  48. static char *ngx_http_userid_mark(ngx_conf_t *cf, ngx_command_t *cmd,
  49.     void *conf);
  50. static ngx_int_t ngx_http_userid_init_worker(ngx_cycle_t *cycle);



  51. static uint32_t  start_value;
  52. static uint32_t  sequencer_v1 = 1;
  53. static uint32_t  sequencer_v2 = 0x03030302;


  54. static u_char expires[] = "; expires=Thu, 31-Dec-37 23:55:55 GMT";


  55. static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;


  56. static ngx_conf_enum_t  ngx_http_userid_state[] = {
  57.     { ngx_string("off"), NGX_HTTP_USERID_OFF },
  58.     { ngx_string("log"), NGX_HTTP_USERID_LOG },
  59.     { ngx_string("v1"), NGX_HTTP_USERID_V1 },
  60.     { ngx_string("on"), NGX_HTTP_USERID_ON },
  61.     { ngx_null_string, 0 }
  62. };


  63. static ngx_conf_post_handler_pt  ngx_http_userid_domain_p =
  64.     ngx_http_userid_domain;
  65. static ngx_conf_post_handler_pt  ngx_http_userid_path_p = ngx_http_userid_path;
  66. static ngx_conf_post_handler_pt  ngx_http_userid_p3p_p = ngx_http_userid_p3p;


  67. static ngx_command_t  ngx_http_userid_commands[] = {

  68.     { ngx_string("userid"),
  69.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  70.       ngx_conf_set_enum_slot,
  71.       NGX_HTTP_LOC_CONF_OFFSET,
  72.       offsetof(ngx_http_userid_conf_t, enable),
  73.       ngx_http_userid_state },

  74.     { ngx_string("userid_service"),
  75.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  76.       ngx_conf_set_num_slot,
  77.       NGX_HTTP_LOC_CONF_OFFSET,
  78.       offsetof(ngx_http_userid_conf_t, service),
  79.       NULL },

  80.     { ngx_string("userid_name"),
  81.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  82.       ngx_conf_set_str_slot,
  83.       NGX_HTTP_LOC_CONF_OFFSET,
  84.       offsetof(ngx_http_userid_conf_t, name),
  85.       NULL },

  86.     { ngx_string("userid_domain"),
  87.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  88.       ngx_conf_set_str_slot,
  89.       NGX_HTTP_LOC_CONF_OFFSET,
  90.       offsetof(ngx_http_userid_conf_t, domain),
  91.       &ngx_http_userid_domain_p },

  92.     { ngx_string("userid_path"),
  93.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  94.       ngx_conf_set_str_slot,
  95.       NGX_HTTP_LOC_CONF_OFFSET,
  96.       offsetof(ngx_http_userid_conf_t, path),
  97.       &ngx_http_userid_path_p },

  98.     { ngx_string("userid_expires"),
  99.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  100.       ngx_http_userid_expires,
  101.       NGX_HTTP_LOC_CONF_OFFSET,
  102.       0,
  103.       NULL },

  104.     { ngx_string("userid_p3p"),
  105.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  106.       ngx_conf_set_str_slot,
  107.       NGX_HTTP_LOC_CONF_OFFSET,
  108.       offsetof(ngx_http_userid_conf_t, p3p),
  109.       &ngx_http_userid_p3p_p },

  110.     { ngx_string("userid_mark"),
  111.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  112.       ngx_http_userid_mark,
  113.       NGX_HTTP_LOC_CONF_OFFSET,
  114.       0,
  115.       NULL },

  116.       ngx_null_command
  117. };


  118. static ngx_http_module_t  ngx_http_userid_filter_module_ctx = {
  119.     ngx_http_userid_add_variables,         /* preconfiguration */
  120.     ngx_http_userid_init,                  /* postconfiguration */

  121.     NULL,                                  /* create main configuration */
  122.     NULL,                                  /* init main configuration */

  123.     NULL,                                  /* create server configuration */
  124.     NULL,                                  /* merge server configuration */

  125.     ngx_http_userid_create_conf,           /* create location configuration */
  126.     ngx_http_userid_merge_conf             /* merge location configuration */
  127. };


  128. ngx_module_t  ngx_http_userid_filter_module = {
  129.     NGX_MODULE_V1,
  130.     &ngx_http_userid_filter_module_ctx,    /* module context */
  131.     ngx_http_userid_commands,              /* module directives */
  132.     NGX_HTTP_MODULE,                       /* module type */
  133.     NULL,                                  /* init master */
  134.     NULL,                                  /* init module */
  135.     ngx_http_userid_init_worker,           /* init process */
  136.     NULL,                                  /* init thread */
  137.     NULL,                                  /* exit thread */
  138.     NULL,                                  /* exit process */
  139.     NULL,                                  /* exit master */
  140.     NGX_MODULE_V1_PADDING
  141. };


  142. static ngx_str_t   ngx_http_userid_got = ngx_string("uid_got");
  143. static ngx_str_t   ngx_http_userid_set = ngx_string("uid_set");
  144. static ngx_str_t   ngx_http_userid_reset = ngx_string("uid_reset");
  145. static ngx_uint_t  ngx_http_userid_reset_index;


  146. static ngx_int_t
  147. ngx_http_userid_filter(ngx_http_request_t *r)
  148. {
  149.     ngx_http_userid_ctx_t   *ctx;
  150.     ngx_http_userid_conf_t  *conf;

  151.     if (r != r->main) {
  152.         return ngx_http_next_header_filter(r);
  153.     }

  154.     conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);

  155.     if (conf->enable < NGX_HTTP_USERID_V1) {
  156.         return ngx_http_next_header_filter(r);
  157.     }

  158.     ctx = ngx_http_userid_get_uid(r, conf);

  159.     if (ctx == NULL) {
  160.         return NGX_ERROR;
  161.     }

  162.     if (ngx_http_userid_set_uid(r, ctx, conf) == NGX_OK) {
  163.         return ngx_http_next_header_filter(r);
  164.     }

  165.     return NGX_ERROR;
  166. }


  167. static ngx_int_t
  168. ngx_http_userid_got_variable(ngx_http_request_t *r,
  169.     ngx_http_variable_value_t *v, uintptr_t data)
  170. {
  171.     ngx_http_userid_ctx_t   *ctx;
  172.     ngx_http_userid_conf_t  *conf;

  173.     conf = ngx_http_get_module_loc_conf(r->main, ngx_http_userid_filter_module);

  174.     if (conf->enable == NGX_HTTP_USERID_OFF) {
  175.         v->not_found = 1;
  176.         return NGX_OK;
  177.     }

  178.     ctx = ngx_http_userid_get_uid(r->main, conf);

  179.     if (ctx == NULL) {
  180.         return NGX_ERROR;
  181.     }

  182.     if (ctx->uid_got[3] != 0) {
  183.         return ngx_http_userid_variable(r->main, v, &conf->name, ctx->uid_got);
  184.     }

  185.     v->not_found = 1;

  186.     return NGX_OK;
  187. }


  188. static ngx_int_t
  189. ngx_http_userid_set_variable(ngx_http_request_t *r,
  190.     ngx_http_variable_value_t *v, uintptr_t data)
  191. {
  192.     ngx_http_userid_ctx_t   *ctx;
  193.     ngx_http_userid_conf_t  *conf;

  194.     conf = ngx_http_get_module_loc_conf(r->main, ngx_http_userid_filter_module);

  195.     if (conf->enable < NGX_HTTP_USERID_V1) {
  196.         v->not_found = 1;
  197.         return NGX_OK;
  198.     }

  199.     ctx = ngx_http_userid_get_uid(r->main, conf);

  200.     if (ctx == NULL) {
  201.         return NGX_ERROR;
  202.     }

  203.     if (ngx_http_userid_create_uid(r->main, ctx, conf) != NGX_OK) {
  204.         return NGX_ERROR;
  205.     }

  206.     if (ctx->uid_set[3] == 0) {
  207.         v->not_found = 1;
  208.         return NGX_OK;
  209.     }

  210.     return ngx_http_userid_variable(r->main, v, &conf->name, ctx->uid_set);
  211. }


  212. static ngx_http_userid_ctx_t *
  213. ngx_http_userid_get_uid(ngx_http_request_t *r, ngx_http_userid_conf_t *conf)
  214. {
  215.     ngx_int_t                n;
  216.     ngx_str_t                src, dst;
  217.     ngx_table_elt_t        **cookies;
  218.     ngx_http_userid_ctx_t   *ctx;

  219.     ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);

  220.     if (ctx) {
  221.         return ctx;
  222.     }

  223.     if (ctx == NULL) {
  224.         ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_userid_ctx_t));
  225.         if (ctx == NULL) {
  226.             return NULL;
  227.         }

  228.         ngx_http_set_ctx(r, ctx, ngx_http_userid_filter_module);
  229.     }

  230.     n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &conf->name,
  231.                                           &ctx->cookie);
  232.     if (n == NGX_DECLINED) {
  233.         return ctx;
  234.     }

  235.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  236.                    "uid cookie: \"%V\"", &ctx->cookie);

  237.     if (ctx->cookie.len < 22) {
  238.         cookies = r->headers_in.cookies.elts;
  239.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  240.                       "client sent too short userid cookie \"%V\"",
  241.                       &cookies[n]->value);
  242.         return ctx;
  243.     }

  244.     src = ctx->cookie;

  245.     /*
  246.      * we have to limit the encoded string to 22 characters because
  247.      *  1) cookie may be marked by "userid_mark",
  248.      *  2) and there are already the millions cookies with a garbage
  249.      *     instead of the correct base64 trail "=="
  250.      */

  251.     src.len = 22;

  252.     dst.data = (u_char *) ctx->uid_got;

  253.     if (ngx_decode_base64(&dst, &src) == NGX_ERROR) {
  254.         cookies = r->headers_in.cookies.elts;
  255.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  256.                       "client sent invalid userid cookie \"%V\"",
  257.                       &cookies[n]->value);
  258.         return ctx;
  259.     }

  260.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  261.                    "uid: %08XD%08XD%08XD%08XD",
  262.                    ctx->uid_got[0], ctx->uid_got[1],
  263.                    ctx->uid_got[2], ctx->uid_got[3]);

  264.     return ctx;
  265. }


  266. static ngx_int_t
  267. ngx_http_userid_set_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx,
  268.     ngx_http_userid_conf_t *conf)
  269. {
  270.     u_char           *cookie, *p;
  271.     size_t            len;
  272.     ngx_str_t         src, dst;
  273.     ngx_table_elt_t  *set_cookie, *p3p;

  274.     if (ngx_http_userid_create_uid(r, ctx, conf) != NGX_OK) {
  275.         return NGX_ERROR;
  276.     }

  277.     if (ctx->uid_set[3] == 0) {
  278.         return NGX_OK;
  279.     }

  280.     len = conf->name.len + 1 + ngx_base64_encoded_length(16) + conf->path.len;

  281.     if (conf->expires) {
  282.         len += sizeof(expires) - 1 + 2;
  283.     }

  284.     if (conf->domain.len) {
  285.         len += conf->domain.len;
  286.     }

  287.     cookie = ngx_pnalloc(r->pool, len);
  288.     if (cookie == NULL) {
  289.         return NGX_ERROR;
  290.     }

  291.     p = ngx_copy(cookie, conf->name.data, conf->name.len);
  292.     *p++ = '=';

  293.     if (ctx->uid_got[3] == 0 || ctx->reset) {
  294.         src.len = 16;
  295.         src.data = (u_char *) ctx->uid_set;
  296.         dst.data = p;

  297.         ngx_encode_base64(&dst, &src);

  298.         p += dst.len;

  299.         if (conf->mark) {
  300.             *(p - 2) = conf->mark;
  301.         }

  302.     } else {
  303.         p = ngx_cpymem(p, ctx->cookie.data, 22);
  304.         *p++ = conf->mark;
  305.         *p++ = '=';
  306.     }

  307.     if (conf->expires == NGX_HTTP_USERID_MAX_EXPIRES) {
  308.         p = ngx_cpymem(p, expires, sizeof(expires) - 1);

  309.     } else if (conf->expires) {
  310.         p = ngx_cpymem(p, expires, sizeof("; expires=") - 1);
  311.         p = ngx_http_cookie_time(p, ngx_time() + conf->expires);
  312.     }

  313.     p = ngx_copy(p, conf->domain.data, conf->domain.len);

  314.     p = ngx_copy(p, conf->path.data, conf->path.len);

  315.     set_cookie = ngx_list_push(&r->headers_out.headers);
  316.     if (set_cookie == NULL) {
  317.         return NGX_ERROR;
  318.     }

  319.     set_cookie->hash = 1;
  320.     ngx_str_set(&set_cookie->key, "Set-Cookie");
  321.     set_cookie->value.len = p - cookie;
  322.     set_cookie->value.data = cookie;

  323.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  324.                    "uid cookie: \"%V\"", &set_cookie->value);

  325.     if (conf->p3p.len == 0) {
  326.         return NGX_OK;
  327.     }

  328.     p3p = ngx_list_push(&r->headers_out.headers);
  329.     if (p3p == NULL) {
  330.         return NGX_ERROR;
  331.     }

  332.     p3p->hash = 1;
  333.     ngx_str_set(&p3p->key, "P3P");
  334.     p3p->value = conf->p3p;

  335.     return NGX_OK;
  336. }


  337. static ngx_int_t
  338. ngx_http_userid_create_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx,
  339.     ngx_http_userid_conf_t *conf)
  340. {
  341.     ngx_connection_t           *c;
  342.     struct sockaddr_in         *sin;
  343.     ngx_http_variable_value_t  *vv;
  344. #if (NGX_HAVE_INET6)
  345.     u_char                     *p;
  346.     struct sockaddr_in6        *sin6;
  347. #endif

  348.     if (ctx->uid_set[3] != 0) {
  349.         return NGX_OK;
  350.     }

  351.     if (ctx->uid_got[3] != 0) {

  352.         vv = ngx_http_get_indexed_variable(r, ngx_http_userid_reset_index);

  353.         if (vv->len == 0 || (vv->len == 1 && vv->data[0] == '0')) {

  354.             if (conf->mark == '\0'
  355.                 || (ctx->cookie.len > 23
  356.                     && ctx->cookie.data[22] == conf->mark
  357.                     && ctx->cookie.data[23] == '='))
  358.             {
  359.                 return NGX_OK;
  360.             }

  361.             ctx->uid_set[0] = ctx->uid_got[0];
  362.             ctx->uid_set[1] = ctx->uid_got[1];
  363.             ctx->uid_set[2] = ctx->uid_got[2];
  364.             ctx->uid_set[3] = ctx->uid_got[3];

  365.             return NGX_OK;

  366.         } else {
  367.             ctx->reset = 1;

  368.             if (vv->len == 3 && ngx_strncmp(vv->data, "log", 3) == 0) {
  369.                 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
  370.                         "userid cookie \"%V=%08XD%08XD%08XD%08XD\" was reset",
  371.                         &conf->name, ctx->uid_got[0], ctx->uid_got[1],
  372.                         ctx->uid_got[2], ctx->uid_got[3]);
  373.             }
  374.         }
  375.     }

  376.     /*
  377.      * TODO: in the threaded mode the sequencers should be in TLS and their
  378.      * ranges should be divided between threads
  379.      */

  380.     if (conf->enable == NGX_HTTP_USERID_V1) {
  381.         if (conf->service == NGX_CONF_UNSET) {
  382.             ctx->uid_set[0] = 0;
  383.         } else {
  384.             ctx->uid_set[0] = conf->service;
  385.         }
  386.         ctx->uid_set[1] = (uint32_t) ngx_time();
  387.         ctx->uid_set[2] = start_value;
  388.         ctx->uid_set[3] = sequencer_v1;
  389.         sequencer_v1 += 0x100;

  390.     } else {
  391.         if (conf->service == NGX_CONF_UNSET) {

  392.             c = r->connection;

  393.             if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
  394.                 return NGX_ERROR;
  395.             }

  396.             switch (c->local_sockaddr->sa_family) {

  397. #if (NGX_HAVE_INET6)
  398.             case AF_INET6:
  399.                 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;

  400.                 p = (u_char *) &ctx->uid_set[0];

  401.                 *p++ = sin6->sin6_addr.s6_addr[12];
  402.                 *p++ = sin6->sin6_addr.s6_addr[13];
  403.                 *p++ = sin6->sin6_addr.s6_addr[14];
  404.                 *p = sin6->sin6_addr.s6_addr[15];

  405.                 break;
  406. #endif
  407.             default: /* AF_INET */
  408.                 sin = (struct sockaddr_in *) c->local_sockaddr;
  409.                 ctx->uid_set[0] = sin->sin_addr.s_addr;
  410.                 break;
  411.             }

  412.         } else {
  413.             ctx->uid_set[0] = htonl(conf->service);
  414.         }

  415.         ctx->uid_set[1] = htonl((uint32_t) ngx_time());
  416.         ctx->uid_set[2] = htonl(start_value);
  417.         ctx->uid_set[3] = htonl(sequencer_v2);
  418.         sequencer_v2 += 0x100;
  419.         if (sequencer_v2 < 0x03030302) {
  420.             sequencer_v2 = 0x03030302;
  421.         }
  422.     }

  423.     return NGX_OK;
  424. }


  425. static ngx_int_t
  426. ngx_http_userid_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
  427.     ngx_str_t *name, uint32_t *uid)
  428. {
  429.     v->len = name->len + sizeof("=00001111222233334444555566667777") - 1;
  430.     v->data = ngx_pnalloc(r->pool, v->len);
  431.     if (v->data == NULL) {
  432.         return NGX_ERROR;
  433.     }

  434.     v->valid = 1;
  435.     v->no_cacheable = 0;
  436.     v->not_found = 0;

  437.     ngx_sprintf(v->data, "%V=%08XD%08XD%08XD%08XD",
  438.                 name, uid[0], uid[1], uid[2], uid[3]);

  439.     return NGX_OK;
  440. }


  441. static ngx_int_t
  442. ngx_http_userid_reset_variable(ngx_http_request_t *r,
  443.     ngx_http_variable_value_t *v, uintptr_t data)
  444. {
  445.     *v = ngx_http_variable_null_value;

  446.     return NGX_OK;
  447. }


  448. static ngx_int_t
  449. ngx_http_userid_add_variables(ngx_conf_t *cf)
  450. {
  451.     ngx_int_t             n;
  452.     ngx_http_variable_t  *var;

  453.     var = ngx_http_add_variable(cf, &ngx_http_userid_got, 0);
  454.     if (var == NULL) {
  455.         return NGX_ERROR;
  456.     }

  457.     var->get_handler = ngx_http_userid_got_variable;

  458.     var = ngx_http_add_variable(cf, &ngx_http_userid_set, 0);
  459.     if (var == NULL) {
  460.         return NGX_ERROR;
  461.     }

  462.     var->get_handler = ngx_http_userid_set_variable;

  463.     var = ngx_http_add_variable(cf, &ngx_http_userid_reset,
  464.                                 NGX_HTTP_VAR_CHANGEABLE);
  465.     if (var == NULL) {
  466.         return NGX_ERROR;
  467.     }

  468.     var->get_handler = ngx_http_userid_reset_variable;

  469.     n = ngx_http_get_variable_index(cf, &ngx_http_userid_reset);
  470.     if (n == NGX_ERROR) {
  471.         return NGX_ERROR;
  472.     }

  473.     ngx_http_userid_reset_index = n;

  474.     return NGX_OK;
  475. }


  476. static void *
  477. ngx_http_userid_create_conf(ngx_conf_t *cf)
  478. {
  479.     ngx_http_userid_conf_t  *conf;

  480.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_userid_conf_t));
  481.     if (conf == NULL) {
  482.         return NULL;
  483.     }

  484.     /*
  485.      * set by ngx_pcalloc():
  486.      *
  487.      *     conf->name = { 0, NULL };
  488.      *     conf->domain = { 0, NULL };
  489.      *     conf->path = { 0, NULL };
  490.      *     conf->p3p = { 0, NULL };
  491.      */

  492.     conf->enable = NGX_CONF_UNSET_UINT;
  493.     conf->service = NGX_CONF_UNSET;
  494.     conf->expires = NGX_CONF_UNSET;
  495.     conf->mark = (u_char) '\xFF';

  496.     return conf;
  497. }


  498. static char *
  499. ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  500. {
  501.     ngx_http_userid_conf_t *prev = parent;
  502.     ngx_http_userid_conf_t *conf = child;

  503.     ngx_conf_merge_uint_value(conf->enable, prev->enable,
  504.                               NGX_HTTP_USERID_OFF);

  505.     ngx_conf_merge_str_value(conf->name, prev->name, "uid");
  506.     ngx_conf_merge_str_value(conf->domain, prev->domain, "");
  507.     ngx_conf_merge_str_value(conf->path, prev->path, "; path=/");
  508.     ngx_conf_merge_str_value(conf->p3p, prev->p3p, "");

  509.     ngx_conf_merge_value(conf->service, prev->service, NGX_CONF_UNSET);
  510.     ngx_conf_merge_sec_value(conf->expires, prev->expires, 0);

  511.     if (conf->mark == (u_char) '\xFF') {
  512.         if (prev->mark == (u_char) '\xFF') {
  513.             conf->mark = '\0';
  514.         } else {
  515.             conf->mark = prev->mark;
  516.         }
  517.     }

  518.     return NGX_CONF_OK;
  519. }


  520. static ngx_int_t
  521. ngx_http_userid_init(ngx_conf_t *cf)
  522. {
  523.     ngx_http_next_header_filter = ngx_http_top_header_filter;
  524.     ngx_http_top_header_filter = ngx_http_userid_filter;

  525.     return NGX_OK;
  526. }


  527. static char *
  528. ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data)
  529. {
  530.     ngx_str_t  *domain = data;

  531.     u_char  *p, *new;

  532.     if (ngx_strcmp(domain->data, "none") == 0) {
  533.         ngx_str_set(domain, "");
  534.         return NGX_CONF_OK;
  535.     }

  536.     new = ngx_pnalloc(cf->pool, sizeof("; domain=") - 1 + domain->len);
  537.     if (new == NULL) {
  538.         return NGX_CONF_ERROR;
  539.     }

  540.     p = ngx_cpymem(new, "; domain=", sizeof("; domain=") - 1);
  541.     ngx_memcpy(p, domain->data, domain->len);

  542.     domain->len += sizeof("; domain=") - 1;
  543.     domain->data = new;

  544.     return NGX_CONF_OK;
  545. }


  546. static char *
  547. ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data)
  548. {
  549.     ngx_str_t  *path = data;

  550.     u_char  *p, *new;

  551.     new = ngx_pnalloc(cf->pool, sizeof("; path=") - 1 + path->len);
  552.     if (new == NULL) {
  553.         return NGX_CONF_ERROR;
  554.     }

  555.     p = ngx_cpymem(new, "; path=", sizeof("; path=") - 1);
  556.     ngx_memcpy(p, path->data, path->len);

  557.     path->len += sizeof("; path=") - 1;
  558.     path->data = new;

  559.     return NGX_CONF_OK;
  560. }


  561. static char *
  562. ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  563. {
  564.     ngx_http_userid_conf_t *ucf = conf;

  565.     ngx_str_t  *value;

  566.     if (ucf->expires != NGX_CONF_UNSET) {
  567.         return "is duplicate";
  568.     }

  569.     value = cf->args->elts;

  570.     if (ngx_strcmp(value[1].data, "max") == 0) {
  571.         ucf->expires = NGX_HTTP_USERID_MAX_EXPIRES;
  572.         return NGX_CONF_OK;
  573.     }

  574.     if (ngx_strcmp(value[1].data, "off") == 0) {
  575.         ucf->expires = 0;
  576.         return NGX_CONF_OK;
  577.     }

  578.     ucf->expires = ngx_parse_time(&value[1], 1);
  579.     if (ucf->expires == (time_t) NGX_ERROR) {
  580.         return "invalid value";
  581.     }

  582.     return NGX_CONF_OK;
  583. }


  584. static char *
  585. ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data)
  586. {
  587.     ngx_str_t  *p3p = data;

  588.     if (ngx_strcmp(p3p->data, "none") == 0) {
  589.         ngx_str_set(p3p, "");
  590.     }

  591.     return NGX_CONF_OK;
  592. }


  593. static char *
  594. ngx_http_userid_mark(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  595. {
  596.     ngx_http_userid_conf_t *ucf = conf;

  597.     ngx_str_t  *value;

  598.     if (ucf->mark != (u_char) '\xFF') {
  599.         return "is duplicate";
  600.     }

  601.     value = cf->args->elts;

  602.     if (ngx_strcmp(value[1].data, "off") == 0) {
  603.         ucf->mark = '\0';
  604.         return NGX_CONF_OK;
  605.     }

  606.     if (value[1].len != 1
  607.         || !((value[1].data[0] >= '0' && value[1].data[0] <= '9')
  608.               || (value[1].data[0] >= 'A' && value[1].data[0] <= 'Z')
  609.               || (value[1].data[0] >= 'a' && value[1].data[0] <= 'z')
  610.               || value[1].data[0] == '='))
  611.     {
  612.         return "value must be \"off\" or a single letter, digit or \"=\"";
  613.     }

  614.     ucf->mark = value[1].data[0];

  615.     return NGX_CONF_OK;
  616. }


  617. static ngx_int_t
  618. ngx_http_userid_init_worker(ngx_cycle_t *cycle)
  619. {
  620.     struct timeval  tp;

  621.     ngx_gettimeofday(&tp);

  622.     /* use the most significant usec part that fits to 16 bits */
  623.     start_value = ((tp.tv_usec / 20) << 16) | ngx_pid;

  624.     return NGX_OK;
  625. }