~ubuntu-branches/debian/squeeze/nginx/squeeze

« back to all changes in this revision

Viewing changes to src/core/ngx_conf_file.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2009-05-31 18:38:56 UTC
  • mfrom: (1.1.10 upstream) (4.1.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20090531183856-3xhvf6wd0bw5556i
Tags: 0.7.59-1
* New upstream release, first in Debian for the 0.7 branch. Among other
  issues, it also fixes the problem with wildcard dns names used with SSL.
  (Closes: #515904)
* debian/watch: updated.
* debian/postinst: fixed a bashism. (Closes: #507913)
* debian/conf/nginx.conf: removed default_type. (Closes: #509390)
* debian/control: updated Standards-Version to 3.8.1, no changes needed.
* debian/NEWS.Debian: documented the issues with
  server_names_hash_bucket_size. (Closes: #524785)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include <ngx_config.h>
8
8
#include <ngx_core.h>
9
9
 
 
10
#define NGX_CONF_BUFFER  4096
10
11
 
11
12
static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last);
12
13
static ngx_int_t ngx_conf_read_token(ngx_conf_t *cf);
43
44
};
44
45
 
45
46
 
46
 
/* The ten fixed arguments */
 
47
/* The eight fixed arguments */
47
48
 
48
 
static int argument_number[] = {
 
49
static ngx_uint_t argument_number[] = {
49
50
    NGX_CONF_NOARGS,
50
51
    NGX_CONF_TAKE1,
51
52
    NGX_CONF_TAKE2,
58
59
 
59
60
 
60
61
char *
 
62
ngx_conf_param(ngx_conf_t *cf)
 
63
{
 
64
    char             *rv;
 
65
    ngx_str_t        *param;
 
66
    ngx_buf_t         b;
 
67
    ngx_conf_file_t   conf_file;
 
68
 
 
69
    param = &cf->cycle->conf_param;
 
70
 
 
71
    if (param->len == 0) {
 
72
        return NGX_CONF_OK;
 
73
    }
 
74
 
 
75
    ngx_memzero(&conf_file, sizeof(ngx_conf_file_t));
 
76
 
 
77
    ngx_memzero(&b, sizeof(ngx_buf_t));
 
78
 
 
79
    b.start = param->data;
 
80
    b.pos = param->data;
 
81
    b.last = param->data + param->len;
 
82
    b.end = b.last;
 
83
    b.temporary = 1;
 
84
 
 
85
    conf_file.file.fd = NGX_INVALID_FILE;
 
86
    conf_file.file.name.data = NULL;
 
87
    conf_file.line = 0;
 
88
 
 
89
    cf->conf_file = &conf_file;
 
90
    cf->conf_file->buffer = &b;
 
91
 
 
92
    rv = ngx_conf_parse(cf, NULL);
 
93
 
 
94
    cf->conf_file = NULL;
 
95
 
 
96
    return rv;
 
97
}
 
98
 
 
99
 
 
100
char *
61
101
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
62
102
{
63
103
    char             *rv;
64
104
    ngx_fd_t          fd;
65
105
    ngx_int_t         rc;
66
 
    ngx_buf_t        *b;
67
 
    ngx_uint_t        block;
68
 
    ngx_conf_file_t  *prev;
 
106
    ngx_buf_t         buf;
 
107
    ngx_conf_file_t  *prev, conf_file;
 
108
    enum {
 
109
        parse_file = 0,
 
110
        parse_block,
 
111
        parse_param
 
112
    } type;
69
113
 
70
114
#if (NGX_SUPPRESS_WARN)
71
115
    fd = NGX_INVALID_FILE;
86
130
 
87
131
        prev = cf->conf_file;
88
132
 
89
 
        cf->conf_file = ngx_palloc(cf->pool, sizeof(ngx_conf_file_t));
90
 
        if (cf->conf_file == NULL) {
91
 
            return NGX_CONF_ERROR;
92
 
        }
 
133
        cf->conf_file = &conf_file;
93
134
 
94
135
        if (ngx_fd_info(fd, &cf->conf_file->file.info) == -1) {
95
136
            ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
96
137
                          ngx_fd_info_n " \"%s\" failed", filename->data);
97
138
        }
98
139
 
99
 
        b = ngx_calloc_buf(cf->pool);
100
 
        if (b == NULL) {
101
 
            return NGX_CONF_ERROR;
102
 
        }
103
 
 
104
 
        cf->conf_file->buffer = b;
105
 
 
106
 
        b->start = ngx_alloc(ngx_pagesize, cf->log);
107
 
        if (b->start == NULL) {
108
 
            return NGX_CONF_ERROR;
109
 
        }
110
 
 
111
 
        b->pos = b->start;
112
 
        b->last = b->start;
113
 
        b->end = b->last + ngx_pagesize;
114
 
        b->temporary = 1;
 
140
        cf->conf_file->buffer = &buf;
 
141
 
 
142
        buf.start = ngx_alloc(NGX_CONF_BUFFER, cf->log);
 
143
        if (buf.start == NULL) {
 
144
            goto failed;
 
145
        }
 
146
 
 
147
        buf.pos = buf.start;
 
148
        buf.last = buf.start;
 
149
        buf.end = buf.last + NGX_CONF_BUFFER;
 
150
        buf.temporary = 1;
115
151
 
116
152
        cf->conf_file->file.fd = fd;
117
153
        cf->conf_file->file.name.len = filename->len;
120
156
        cf->conf_file->file.log = cf->log;
121
157
        cf->conf_file->line = 1;
122
158
 
123
 
        block = 0;
 
159
        type = parse_file;
 
160
 
 
161
    } else if (cf->conf_file->file.fd != NGX_INVALID_FILE) {
 
162
 
 
163
        type = parse_block;
124
164
 
125
165
    } else {
126
 
        block = 1;
 
166
        type = parse_param;
127
167
    }
128
168
 
129
169
 
145
185
        }
146
186
 
147
187
        if (rc == NGX_CONF_BLOCK_DONE) {
148
 
            if (!block) {
 
188
 
 
189
            if (type != parse_block) {
149
190
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unexpected \"}\"");
150
191
                goto failed;
151
192
            }
152
193
 
153
 
            block = 0;
154
 
        }
155
 
 
156
 
        if (rc == NGX_CONF_FILE_DONE && block) {
157
 
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
158
 
                               "unexpected end of file, expecting \"}\"");
159
 
            goto failed;
160
 
        }
161
 
 
162
 
        if (rc != NGX_OK && rc != NGX_CONF_BLOCK_START) {
163
 
            goto done;
164
 
        }
 
194
            goto done;
 
195
        }
 
196
 
 
197
        if (rc == NGX_CONF_FILE_DONE) {
 
198
 
 
199
            if (type == parse_block) {
 
200
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
201
                                   "unexpected end of file, expecting \"}\"");
 
202
                goto failed;
 
203
            }
 
204
 
 
205
            goto done;
 
206
        }
 
207
 
 
208
        if (rc == NGX_CONF_BLOCK_START) {
 
209
 
 
210
            if (type == parse_param) {
 
211
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
212
                                   "block directives are not supported "
 
213
                                   "in -g option");
 
214
                goto failed;
 
215
            }
 
216
        }
 
217
 
 
218
        /* rc == NGX_OK || rc == NGX_CONF_BLOCK_START */
165
219
 
166
220
        if (cf->handler) {
167
221
 
199
253
done:
200
254
 
201
255
    if (filename) {
202
 
        ngx_free(cf->conf_file->buffer->start);
203
 
 
204
 
        cf->conf_file = prev;
 
256
        if (cf->conf_file->buffer->start) {
 
257
            ngx_free(cf->conf_file->buffer->start);
 
258
        }
205
259
 
206
260
        if (ngx_close_file(fd) == NGX_FILE_ERROR) {
207
261
            ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
209
263
                          cf->conf_file->file.name.data);
210
264
            return NGX_CONF_ERROR;
211
265
        }
 
266
 
 
267
        cf->conf_file = prev;
212
268
    }
213
269
 
214
270
    if (rc == NGX_ERROR) {
377
433
ngx_conf_read_token(ngx_conf_t *cf)
378
434
{
379
435
    u_char      *start, ch, *src, *dst;
380
 
    int          len;
381
 
    int          found, need_space, last_space, sharp_comment, variable;
382
 
    int          quoted, s_quoted, d_quoted;
383
 
    ssize_t      n;
 
436
    off_t        file_size;
 
437
    size_t       len;
 
438
    ssize_t      n, size;
 
439
    ngx_uint_t   found, need_space, last_space, sharp_comment, variable;
 
440
    ngx_uint_t   quoted, s_quoted, d_quoted, start_line;
384
441
    ngx_str_t   *word;
385
442
    ngx_buf_t   *b;
386
443
 
389
446
    last_space = 1;
390
447
    sharp_comment = 0;
391
448
    variable = 0;
392
 
    quoted = s_quoted = d_quoted = 0;
 
449
    quoted = 0;
 
450
    s_quoted = 0;
 
451
    d_quoted = 0;
393
452
 
394
453
    cf->args->nelts = 0;
395
454
    b = cf->conf_file->buffer;
396
455
    start = b->pos;
 
456
    start_line = cf->conf_file->line;
 
457
 
 
458
    file_size = ngx_file_size(&cf->conf_file->file.info);
397
459
 
398
460
    for ( ;; ) {
399
461
 
400
462
        if (b->pos >= b->last) {
401
 
            if (cf->conf_file->file.offset
402
 
                                 >= ngx_file_size(&cf->conf_file->file.info))
403
 
            {
 
463
 
 
464
            if (cf->conf_file->file.offset >= file_size) {
 
465
 
404
466
                if (cf->args->nelts > 0) {
 
467
 
 
468
                    if (cf->conf_file->file.fd == NGX_INVALID_FILE) {
 
469
                        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
470
                                           "unexpected end of parameter, "
 
471
                                           "expecting \";\"");
 
472
                        return NGX_ERROR;
 
473
                    }
 
474
 
405
475
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
406
476
                                  "unexpected end of file, "
407
477
                                  "expecting \";\" or \"}\"");
411
481
                return NGX_CONF_FILE_DONE;
412
482
            }
413
483
 
414
 
            if (b->pos - start) {
415
 
                ngx_memcpy(b->start, start, b->pos - start);
416
 
            }
417
 
 
418
 
            n = ngx_read_file(&cf->conf_file->file,
419
 
                              b->start + (b->pos - start),
420
 
                              b->end - (b->start + (b->pos - start)),
 
484
            len = b->pos - start;
 
485
 
 
486
            if (len == NGX_CONF_BUFFER) {
 
487
                cf->conf_file->line = start_line;
 
488
 
 
489
                if (d_quoted) {
 
490
                    ch = '"';
 
491
 
 
492
                } else if (s_quoted) {
 
493
                    ch = '\'';
 
494
 
 
495
                } else {
 
496
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
497
                                       "too long parameter \"%*s...\" started",
 
498
                                       10, start);
 
499
                    return NGX_ERROR;
 
500
                }
 
501
 
 
502
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
503
                                   "too long parameter, probably "
 
504
                                   "missing terminating \"%c\" character", ch);
 
505
                return NGX_ERROR;
 
506
            }
 
507
 
 
508
            if (len) {
 
509
                ngx_memcpy(b->start, start, len);
 
510
            }
 
511
 
 
512
            size = (ssize_t) (file_size - cf->conf_file->file.offset);
 
513
 
 
514
            if (size > b->end - (b->start + len)) {
 
515
                size = b->end - (b->start + len);
 
516
            }
 
517
 
 
518
            n = ngx_read_file(&cf->conf_file->file, b->start + len, size,
421
519
                              cf->conf_file->file.offset);
422
520
 
423
521
            if (n == NGX_ERROR) {
424
522
                return NGX_ERROR;
425
523
            }
426
524
 
427
 
            b->pos = b->start + (b->pos - start);
 
525
            if (n != size) {
 
526
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
527
                                   ngx_read_file_n " returned "
 
528
                                   "only %z bytes instead of %z",
 
529
                                   n, size);
 
530
                return NGX_ERROR;
 
531
            }
 
532
 
 
533
            b->pos = b->start + len;
 
534
            b->last = b->pos + n;
428
535
            start = b->start;
429
 
            b->last = b->pos + n;
430
536
        }
431
537
 
432
538
        ch = *b->pos++;
480
586
            }
481
587
 
482
588
            start = b->pos - 1;
 
589
            start_line = cf->conf_file->line;
483
590
 
484
591
            switch (ch) {
485
592
 
574
681
                    return NGX_ERROR;
575
682
                }
576
683
 
577
 
                word->data = ngx_palloc(cf->pool, b->pos - start + 1);
 
684
                word->data = ngx_pnalloc(cf->pool, b->pos - start + 1);
578
685
                if (word->data == NULL) {
579
686
                    return NGX_ERROR;
580
687
                }
641
748
 
642
749
    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
643
750
 
644
 
    if (ngx_conf_full_name(cf->cycle, &file, 1) == NGX_ERROR) {
 
751
    if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
645
752
        return NGX_CONF_ERROR;
646
753
    }
647
754
 
698
805
    u_char     *p, *prefix;
699
806
    ngx_str_t   old;
700
807
 
701
 
    if (name->data[0] == '/') {
702
 
        return NGX_OK;
703
 
    }
704
 
 
705
808
#if (NGX_WIN32)
706
809
 
707
810
    if (name->len > 2
712
815
        return NGX_OK;
713
816
    }
714
817
 
 
818
#else
 
819
 
 
820
    if (name->data[0] == '/') {
 
821
        return NGX_OK;
 
822
    }
 
823
 
715
824
#endif
716
825
 
717
826
    old = *name;
718
827
 
719
828
    if (conf_prefix) {
720
 
        len = sizeof(NGX_CONF_PREFIX) - 1;
721
 
        prefix = (u_char *) NGX_CONF_PREFIX;
 
829
        len = cycle->conf_prefix.len;
 
830
        prefix = cycle->conf_prefix.data;
722
831
 
723
832
    } else {
724
 
        len = cycle->root.len;
725
 
        prefix = cycle->root.data;
 
833
        len = cycle->prefix.len;
 
834
        prefix = cycle->prefix.data;
726
835
    }
727
836
 
728
837
    name->len = len + old.len;
729
 
    name->data = ngx_palloc(cycle->pool, name->len + 1);
 
838
    name->data = ngx_pnalloc(cycle->pool, name->len + 1);
730
839
    if (name->data == NULL) {
731
 
        return  NGX_ERROR;
 
840
        return NGX_ERROR;
732
841
    }
733
842
 
734
843
    p = ngx_cpymem(name->data, prefix, len);
751
860
    full.data = NULL;
752
861
#endif
753
862
 
754
 
    if (name) {
 
863
    if (name->len) {
755
864
        full = *name;
756
865
 
757
 
        if (ngx_conf_full_name(cycle, &full, 0) == NGX_ERROR) {
 
866
        if (ngx_conf_full_name(cycle, &full, 0) != NGX_OK) {
758
867
            return NULL;
759
868
        }
760
869
 
787
896
        return NULL;
788
897
    }
789
898
 
790
 
    if (name) {
 
899
    if (name->len) {
791
900
        file->fd = NGX_INVALID_FILE;
792
901
        file->name = full;
793
902
 
794
903
    } else {
795
 
        file->fd = ngx_stderr_fileno;
796
 
        file->name.len = 0;
797
 
        file->name.data = NULL;
 
904
        file->fd = ngx_stderr;
 
905
        file->name = *name;
798
906
    }
799
907
 
800
908
    file->buffer = NULL;
853
961
ngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,
854
962
    char *fmt, ...)
855
963
{
856
 
    u_char   errstr[NGX_MAX_CONF_ERRSTR], *buf, *last;
 
964
    u_char   errstr[NGX_MAX_CONF_ERRSTR], *p, *last;
857
965
    va_list  args;
858
966
 
859
967
    last = errstr + NGX_MAX_CONF_ERRSTR;
860
968
 
861
969
    va_start(args, fmt);
862
 
    buf = ngx_vsnprintf(errstr, last - errstr, fmt, args);
 
970
    p = ngx_vslprintf(errstr, last, fmt, args);
863
971
    va_end(args);
864
972
 
865
 
    *buf = '\0';
866
 
 
867
973
    if (err) {
868
 
        buf = ngx_snprintf(buf, last - buf - 1, " (%d: ", err);
869
 
        buf = ngx_strerror_r(err, buf, last - buf - 1);
870
 
        *buf++ = ')';
871
 
        *buf = '\0';
 
974
        p = ngx_log_errno(p, last, err);
872
975
    }
873
976
 
874
977
    if (cf->conf_file == NULL) {
875
 
        ngx_log_error(level, cf->log, 0, "%s", errstr);
876
 
        return;
877
 
    }
878
 
 
879
 
    ngx_log_error(level, cf->log, 0, "%s in %s:%ui",
880
 
                  errstr, cf->conf_file->file.name.data, cf->conf_file->line);
 
978
        ngx_log_error(level, cf->log, 0, "%*s", p - errstr, errstr);
 
979
        return;
 
980
    }
 
981
 
 
982
    if (cf->conf_file->file.fd == NGX_INVALID_FILE) {
 
983
        ngx_log_error(level, cf->log, 0, "%*s in command line",
 
984
                      p - errstr, errstr);
 
985
        return;
 
986
    }
 
987
 
 
988
    ngx_log_error(level, cf->log, 0, "%*s in %s:%ui",
 
989
                  p - errstr, errstr,
 
990
                  cf->conf_file->file.name.data, cf->conf_file->line);
881
991
}
882
992
 
883
993