~ubuntu-branches/ubuntu/trusty/nginx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/http/modules/ngx_http_gzip_static_module.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-04-25 12:51:45 UTC
  • mfrom: (1.3.28)
  • mto: (1.3.29) (15.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: package-import@ubuntu.com-20130425125145-ugl0wor6bq0u5eae
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
3
 * Copyright (C) Igor Sysoev
 
4
 * Copyright (C) Nginx, Inc.
4
5
 */
5
6
 
6
7
 
9
10
#include <ngx_http.h>
10
11
 
11
12
 
 
13
#define NGX_HTTP_GZIP_STATIC_OFF     0
 
14
#define NGX_HTTP_GZIP_STATIC_ON      1
 
15
#define NGX_HTTP_GZIP_STATIC_ALWAYS  2
 
16
 
 
17
 
12
18
typedef struct {
13
 
    ngx_flag_t  enable;
 
19
    ngx_uint_t  enable;
14
20
} ngx_http_gzip_static_conf_t;
15
21
 
16
22
 
21
27
static ngx_int_t ngx_http_gzip_static_init(ngx_conf_t *cf);
22
28
 
23
29
 
 
30
static ngx_conf_enum_t  ngx_http_gzip_static[] = {
 
31
    { ngx_string("off"), NGX_HTTP_GZIP_STATIC_OFF },
 
32
    { ngx_string("on"), NGX_HTTP_GZIP_STATIC_ON },
 
33
    { ngx_string("always"), NGX_HTTP_GZIP_STATIC_ALWAYS },
 
34
    { ngx_null_string, 0 }
 
35
};
 
36
 
 
37
 
24
38
static ngx_command_t  ngx_http_gzip_static_commands[] = {
25
39
 
26
40
    { ngx_string("gzip_static"),
27
41
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
28
 
      ngx_conf_set_flag_slot,
 
42
      ngx_conf_set_enum_slot,
29
43
      NGX_HTTP_LOC_CONF_OFFSET,
30
44
      offsetof(ngx_http_gzip_static_conf_t, enable),
31
 
      NULL },
 
45
      &ngx_http_gzip_static },
32
46
 
33
47
      ngx_null_command
34
48
};
89
103
        return NGX_DECLINED;
90
104
    }
91
105
 
92
 
    /* TODO: Win32 */
93
 
    if (r->zero_in_uri) {
94
 
        return NGX_DECLINED;
95
 
    }
96
 
 
97
106
    gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
98
107
 
99
 
    if (!gzcf->enable || ngx_http_gzip_ok(r) != NGX_OK) {
 
108
    if (gzcf->enable == NGX_HTTP_GZIP_STATIC_OFF) {
 
109
        return NGX_DECLINED;
 
110
    }
 
111
 
 
112
    if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) {
 
113
        rc = ngx_http_gzip_ok(r);
 
114
 
 
115
    } else {
 
116
        /* always */
 
117
        rc = NGX_OK;
 
118
    }
 
119
 
 
120
    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
121
 
 
122
    if (!clcf->gzip_vary && rc != NGX_OK) {
100
123
        return NGX_DECLINED;
101
124
    }
102
125
 
117
140
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
118
141
                   "http filename: \"%s\"", path.data);
119
142
 
120
 
    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
143
    ngx_memzero(&of, sizeof(ngx_open_file_info_t));
121
144
 
122
 
    of.test_dir = 0;
 
145
    of.read_ahead = clcf->read_ahead;
 
146
    of.directio = clcf->directio;
123
147
    of.valid = clcf->open_file_cache_valid;
124
148
    of.min_uses = clcf->open_file_cache_min_uses;
125
149
    of.errors = clcf->open_file_cache_errors;
126
150
    of.events = clcf->open_file_cache_events;
127
151
 
 
152
    if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
 
153
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
154
    }
 
155
 
128
156
    if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
129
157
        != NGX_OK)
130
158
    {
140
168
            return NGX_DECLINED;
141
169
 
142
170
        case NGX_EACCES:
 
171
#if (NGX_HAVE_OPENAT)
 
172
        case NGX_EMLINK:
 
173
        case NGX_ELOOP:
 
174
#endif
143
175
 
144
176
            level = NGX_LOG_ERR;
145
177
            break;
151
183
        }
152
184
 
153
185
        ngx_log_error(level, log, of.err,
154
 
                      ngx_open_file_n " \"%s\" failed", path.data);
 
186
                      "%s \"%s\" failed", of.failed, path.data);
155
187
 
156
188
        return NGX_DECLINED;
157
189
    }
158
190
 
 
191
    if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) {
 
192
        r->gzip_vary = 1;
 
193
 
 
194
        if (rc != NGX_OK) {
 
195
            return NGX_DECLINED;
 
196
        }
 
197
    }
 
198
 
159
199
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
160
200
 
161
201
    if (of.is_dir) {
166
206
#if !(NGX_WIN32) /* the not regular files are probably Unix specific */
167
207
 
168
208
    if (!of.is_file) {
169
 
        ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
 
209
        ngx_log_error(NGX_LOG_CRIT, log, 0,
170
210
                      "\"%s\" is not a regular file", path.data);
171
211
 
172
212
        return NGX_HTTP_NOT_FOUND;
174
214
 
175
215
#endif
176
216
 
 
217
    r->root_tested = !r->error_page;
 
218
 
177
219
    rc = ngx_http_discard_request_body(r);
178
220
 
179
221
    if (rc != NGX_OK) {
186
228
    r->headers_out.content_length_n = of.size;
187
229
    r->headers_out.last_modified_time = of.mtime;
188
230
 
 
231
    if (ngx_http_set_etag(r) != NGX_OK) {
 
232
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
233
    }
 
234
 
189
235
    if (ngx_http_set_content_type(r) != NGX_OK) {
190
236
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
191
237
    }
196
242
    }
197
243
 
198
244
    h->hash = 1;
199
 
    h->key.len = sizeof("Content-Encoding") - 1;
200
 
    h->key.data = (u_char *) "Content-Encoding";
201
 
    h->value.len = sizeof("gzip") - 1;
202
 
    h->value.data = (u_char *) "gzip";
203
 
 
 
245
    ngx_str_set(&h->key, "Content-Encoding");
 
246
    ngx_str_set(&h->value, "gzip");
204
247
    r->headers_out.content_encoding = h;
205
248
 
 
249
    r->ignore_content_encoding = 1;
 
250
 
206
251
    /* we need to allocate all before the header would be sent */
207
252
 
208
253
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
225
270
    b->file_last = of.size;
226
271
 
227
272
    b->in_file = b->file_last ? 1 : 0;
228
 
    b->last_buf = 1;
 
273
    b->last_buf = (r == r->main) ? 1 : 0;
229
274
    b->last_in_chain = 1;
230
275
 
231
276
    b->file->fd = of.fd;
232
277
    b->file->name = path;
233
278
    b->file->log = log;
 
279
    b->file->directio = of.is_directio;
234
280
 
235
281
    out.buf = b;
236
282
    out.next = NULL;
246
292
 
247
293
    conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
248
294
    if (conf == NULL) {
249
 
        return NGX_CONF_ERROR;
 
295
        return NULL;
250
296
    }
251
297
 
252
 
    conf->enable = NGX_CONF_UNSET;
 
298
    conf->enable = NGX_CONF_UNSET_UINT;
253
299
 
254
300
    return conf;
255
301
}
261
307
    ngx_http_gzip_static_conf_t *prev = parent;
262
308
    ngx_http_gzip_static_conf_t *conf = child;
263
309
 
264
 
    ngx_conf_merge_value(conf->enable, prev->enable, 0);
 
310
    ngx_conf_merge_uint_value(conf->enable, prev->enable,
 
311
                              NGX_HTTP_GZIP_STATIC_OFF);
265
312
 
266
313
    return NGX_CONF_OK;
267
314
}