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

Global variables defined

Functions 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. static char *ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd,
  9.     void *conf);

  10. static ngx_command_t  ngx_http_empty_gif_commands[] = {

  11.     { ngx_string("empty_gif"),
  12.       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
  13.       ngx_http_empty_gif,
  14.       0,
  15.       0,
  16.       NULL },

  17.       ngx_null_command
  18. };


  19. /* the minimal single pixel transparent GIF, 43 bytes */

  20. static u_char  ngx_empty_gif[] = {

  21.     'G', 'I', 'F', '8', '9', 'a'/* header                                 */

  22.                                    /* logical screen descriptor              */
  23.     0x01, 0x00,                    /* logical screen width                   */
  24.     0x01, 0x00,                    /* logical screen height                  */
  25.     0x80,                          /* global 1-bit color table               */
  26.     0x01,                          /* background color #1                    */
  27.     0x00,                          /* no aspect ratio                        */

  28.                                    /* global color table                     */
  29.     0x00, 0x00, 0x00,              /* #0: black                              */
  30.     0xff, 0xff, 0xff,              /* #1: white                              */

  31.                                    /* graphic control extension              */
  32.     0x21,                          /* extension introducer                   */
  33.     0xf9,                          /* graphic control label                  */
  34.     0x04,                          /* block size                             */
  35.     0x01,                          /* transparent color is given,            */
  36.                                    /*     no disposal specified,             */
  37.                                    /*     user input is not expected         */
  38.     0x00, 0x00,                    /* delay time                             */
  39.     0x01,                          /* transparent color #1                   */
  40.     0x00,                          /* block terminator                       */

  41.                                    /* image descriptor                       */
  42.     0x2c,                          /* image separator                        */
  43.     0x00, 0x00,                    /* image left position                    */
  44.     0x00, 0x00,                    /* image top position                     */
  45.     0x01, 0x00,                    /* image width                            */
  46.     0x01, 0x00,                    /* image height                           */
  47.     0x00,                          /* no local color table, no interlaced    */

  48.                                    /* table based image data                 */
  49.     0x02,                          /* LZW minimum code size,                 */
  50.                                    /*     must be at least 2-bit             */
  51.     0x02,                          /* block size                             */
  52.     0x4c, 0x01,                    /* compressed bytes 01_001_100, 0000000_1 */
  53.                                    /* 100: clear code                        */
  54.                                    /* 001: 1                                 */
  55.                                    /* 101: end of information code           */
  56.     0x00,                          /* block terminator                       */

  57.     0x3B                           /* trailer                                */
  58. };


  59. static ngx_http_module_t  ngx_http_empty_gif_module_ctx = {
  60.     NULL,                          /* preconfiguration */
  61.     NULL,                          /* postconfiguration */

  62.     NULL,                          /* create main configuration */
  63.     NULL,                          /* init main configuration */

  64.     NULL,                          /* create server configuration */
  65.     NULL,                          /* merge server configuration */

  66.     NULL,                          /* create location configuration */
  67.     NULL                           /* merge location configuration */
  68. };


  69. ngx_module_t  ngx_http_empty_gif_module = {
  70.     NGX_MODULE_V1,
  71.     &ngx_http_empty_gif_module_ctx, /* module context */
  72.     ngx_http_empty_gif_commands,   /* module directives */
  73.     NGX_HTTP_MODULE,               /* module type */
  74.     NULL,                          /* init master */
  75.     NULL,                          /* init module */
  76.     NULL,                          /* init process */
  77.     NULL,                          /* init thread */
  78.     NULL,                          /* exit thread */
  79.     NULL,                          /* exit process */
  80.     NULL,                          /* exit master */
  81.     NGX_MODULE_V1_PADDING
  82. };


  83. static ngx_str_t  ngx_http_gif_type = ngx_string("image/gif");


  84. static ngx_int_t
  85. ngx_http_empty_gif_handler(ngx_http_request_t *r)
  86. {
  87.     ngx_http_complex_value_t  cv;

  88.     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
  89.         return NGX_HTTP_NOT_ALLOWED;
  90.     }

  91.     ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));

  92.     cv.value.len = sizeof(ngx_empty_gif);
  93.     cv.value.data = ngx_empty_gif;
  94.     r->headers_out.last_modified_time = 23349600;

  95.     return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_http_gif_type, &cv);
  96. }


  97. static char *
  98. ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  99. {
  100.     ngx_http_core_loc_conf_t  *clcf;

  101.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  102.     clcf->handler = ngx_http_empty_gif_handler;

  103.     return NGX_CONF_OK;
  104. }