3 * Copyright (C) Igor Sysoev
6 #include <ngx_config.h>
11 static char *ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd,
14 static ngx_command_t ngx_http_empty_gif_commands[] = {
16 { ngx_string("empty_gif"),
17 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
27 /* the minimal single pixel transparent GIF, 43 bytes */
29 static u_char ngx_empty_gif[] = {
31 'G', 'I', 'F', '8', '9', 'a', /* header */
33 /* logical screen descriptor */
34 0x01, 0x00, /* logical screen width */
35 0x01, 0x00, /* logical screen height */
36 0x80, /* global 1-bit color table */
37 0x01, /* background color #1 */
38 0x00, /* no aspect ratio */
40 /* global color table */
41 0x00, 0x00, 0x00, /* #0: black */
42 0xff, 0xff, 0xff, /* #1: white */
44 /* graphic control extension */
45 0x21, /* extension introducer */
46 0xf9, /* graphic control label */
47 0x04, /* block size */
48 0x01, /* transparent color is given, */
49 /* no disposal specified, */
50 /* user input is not expected */
51 0x00, 0x00, /* delay time */
52 0x01, /* transparent color #1 */
53 0x00, /* block terminator */
55 /* image descriptor */
56 0x2c, /* image separator */
57 0x00, 0x00, /* image left position */
58 0x00, 0x00, /* image top position */
59 0x01, 0x00, /* image width */
60 0x01, 0x00, /* image height */
61 0x00, /* no local color table, no interlaced */
63 /* table based image data */
64 0x02, /* LZW minimum code size, */
65 /* must be at least 2-bit */
66 0x02, /* block size */
67 0x4c, 0x01, /* compressed bytes 01_001_100, 0000000_1 */
70 /* 101: end of information code */
71 0x00, /* block terminator */
77 static ngx_http_module_t ngx_http_empty_gif_module_ctx = {
78 NULL, /* preconfiguration */
79 NULL, /* postconfiguration */
81 NULL, /* create main configuration */
82 NULL, /* init main configuration */
84 NULL, /* create server configuration */
85 NULL, /* merge server configuration */
87 NULL, /* create location configuration */
88 NULL /* merge location configuration */
92 ngx_module_t ngx_http_empty_gif_module = {
94 &ngx_http_empty_gif_module_ctx, /* module context */
95 ngx_http_empty_gif_commands, /* module directives */
96 NGX_HTTP_MODULE, /* module type */
97 NULL, /* init master */
98 NULL, /* init module */
99 NULL, /* init process */
100 NULL, /* init thread */
101 NULL, /* exit thread */
102 NULL, /* exit process */
103 NULL, /* exit master */
104 NGX_MODULE_V1_PADDING
109 ngx_http_empty_gif_handler(ngx_http_request_t *r)
115 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
116 return NGX_HTTP_NOT_ALLOWED;
119 rc = ngx_http_discard_request_body(r);
125 r->headers_out.content_type_len = sizeof("image/gif") - 1;
126 r->headers_out.content_type.len = sizeof("image/gif") - 1;
127 r->headers_out.content_type.data = (u_char *) "image/gif";
129 if (r->method == NGX_HTTP_HEAD) {
130 r->headers_out.status = NGX_HTTP_OK;
131 r->headers_out.content_length_n = sizeof(ngx_empty_gif);
132 r->headers_out.last_modified_time = 23349600;
134 return ngx_http_send_header(r);
137 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
139 return NGX_HTTP_INTERNAL_SERVER_ERROR;
145 b->pos = ngx_empty_gif;
146 b->last = ngx_empty_gif + sizeof(ngx_empty_gif);
150 r->headers_out.status = NGX_HTTP_OK;
151 r->headers_out.content_length_n = sizeof(ngx_empty_gif);
152 r->headers_out.last_modified_time = 23349600;
154 rc = ngx_http_send_header(r);
156 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
160 return ngx_http_output_filter(r, &out);
165 ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
167 ngx_http_core_loc_conf_t *clcf;
169 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
170 clcf->handler = ngx_http_empty_gif_handler;