Advertisement
Guest User

Untitled

a guest
Dec 11th, 2014
231
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None
  1. /*
  2.  * ngx_http_hello_dolly.c
  3.  */
  4.  
  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8.  
  9. static char *ngx_http_hello_dolly(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  10.  
  11. /* commands made available in the location config */
  12. static ngx_command_t ngx_http_hello_dolly_commands[] = {
  13.     {
  14.         ngx_string("hello_dolly"),          // the directive
  15.         NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,  // lives in a location block, and doesn't take any args
  16.         ngx_http_hello_dolly,               // function callback
  17.         0,                                  // setup
  18.         0,                                  // offset
  19.         NULL,                               //post-processing
  20.     },
  21.  
  22.     ngx_null_command
  23. };
  24.  
  25. /* hello dolly contents */
  26. static char *ngx_hello_dolly_strings[] = {
  27.     "Hello, Dolly",
  28.     "It's so nice to have you back where you belong",
  29.     "You're lookin' swell, Dolly",
  30.     "I can tell, Dolly"
  31. };
  32.  
  33. /* we don't need to define any config handlers */
  34. static ngx_http_module_t ngx_http_hello_dolly_module_ctx = {
  35.     NULL,
  36.     NULL,
  37.  
  38.     NULL,
  39.     NULL,
  40.  
  41.     NULL,
  42.     NULL,
  43.  
  44.     NULL,
  45.     NULL
  46. };
  47.  
  48. /* expose the module to nginx */
  49. ngx_module_t ngx_http_hello_dolly_module = {
  50.     NGX_MODULE_V1,
  51.     &ngx_http_hello_dolly_module_ctx,   // module context
  52.     ngx_http_hello_dolly_commands,      // module commands
  53.     NGX_HTTP_MODULE,                    // module type
  54.     NULL,                               // more handlers
  55.     NULL,
  56.     NULL,
  57.     NULL,
  58.     NULL,
  59.     NULL,
  60.     NULL,
  61.     NGX_MODULE_V1_PADDING               // lol no idea what the hell this is
  62. };
  63.  
  64. /* this is where the magic happens */
  65. static ngx_int_t ngx_http_hello_dolly_handler(ngx_http_request_t *r) {
  66.     ngx_int_t rc;
  67.     ngx_buf_t *b;
  68.     ngx_chain_t out;
  69.  
  70.     // get a random item from the array. yes i know this is a bad way to do it.
  71.     u_char *hello_dolly_str = (u_char *) ngx_hello_dolly_strings[0];
  72.  
  73.     // respond to GET and HEAD requests only
  74.     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
  75.         return NGX_HTTP_NOT_ALLOWED;
  76.     }
  77.  
  78.     // discard the request body
  79.     rc = ngx_http_discard_request_body(r);
  80.     if (rc != NGX_OK) {
  81.         return rc;
  82.     }
  83.  
  84.     // set the Content-Type, Content-Length, and Status headers
  85.     r->headers_out.content_type_len = sizeof("text/plain") - 1;
  86.     r->headers_out.content_type.len = sizeof("text/plain") - 1;
  87.     r->headers_out.content_type.data = (u_char *) "text/plain";
  88.  
  89.     r->headers_out.content_length_n = sizeof(hello_dolly_str) - 1;
  90.     r->headers_out.status = NGX_HTTP_OK;
  91.    
  92.     // if this is a HEAD request, just send the header
  93.     if (r->method == NGX_HTTP_HEAD) {
  94.         return ngx_http_send_header(r);
  95.     }
  96.  
  97.     // allocate a buffer for the response body
  98.     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
  99.     if (b == NULL) {
  100.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  101.     }
  102.  
  103.     // attach our buffer to the buffer chain
  104.     out.buf = b;
  105.     out.next = NULL;
  106.  
  107.     // adjust the buffer pointers
  108.     b->pos = hello_dolly_str;
  109.     b->last = hello_dolly_str + sizeof(hello_dolly_str) - 1;
  110.     b->memory = 1;      // this buffer is in memory
  111.     b->last_buf = 1;    // this is the last buffer in the buffer chain
  112.  
  113.     rc = ngx_http_send_header(r);
  114.  
  115.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  116.         return rc;
  117.     }
  118.  
  119.     return ngx_http_output_filter(r, &out);
  120. }
  121.  
  122. static char *ngx_http_hello_dolly(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
  123.     ngx_http_core_loc_conf_t *clcf;
  124.  
  125.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  126.     clcf->handler = ngx_http_hello_dolly_handler;
  127.  
  128.     return NGX_CONF_OK;
  129. }
Advertisement
RAW Paste Data Copied
Advertisement