1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
 * Nginx module to fetch static files concatenating chunks specified by an XML.
 *
 * Copyright (c) 2013 Ruslan Osmanov <rrosmanov at gmail dot com>
 */


/* XXX libxml2(as well as any XML stuff) requires UTF-8 encoding.
 * So we have to check whether `charset` is set to whether 'utf-8', or 'utf-16'.
 * However, libxml2 converts encoding automatically, if encoding is specified
 * explicitly in the XML document!
 * Thus, we'll try to parse XML as is, then, in case of a parse error, we'll
 * check for
 * a) whether encoding specified explicitly(xmlDoc.encoding), and
 * b) whether 'charset' is a Unicode */


#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

#include <libxml/xmlstring.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>


typedef struct {
    u_char *name;
    void   *data;
} ngx_http_file_chunks_file_t;

typedef struct {
    ngx_array_t dtd_files;   /* ngx_http_file_chunks_file_t */
} ngx_http_file_chunks_main_conf_t;

typedef struct {
    ngx_flag_t   enable;
    ngx_hash_t   input_types;
    ngx_array_t *input_type_keys;
    xmlDtdPtr    dtd;
} ngx_http_file_chunks_conf_t;

typedef struct {
    ngx_uint_t          done;      /* unsigned:1                                           */
    ngx_http_request_t *request;
    xmlDocPtr           doc;
    xmlParserCtxtPtr    ctxt;      /* XML parser context for using the parser in push mode */
} ngx_http_file_chunks_ctx_t;


static ngx_int_t ngx_http_file_chunks_filter_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_file_chunks_filter_preconfigure(ngx_conf_t *cf);
static void ngx_http_file_chunks_filter_exit(ngx_cycle_t *cycle);
static void *ngx_http_file_chunks_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_file_chunks_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_file_chunks_merge_loc_conf(ngx_conf_t *cf, void *parent,
        void *child);
static ngx_int_t ngx_http_file_chunks_parse_chunk(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx, ngx_buf_t *b);
static ngx_int_t ngx_http_file_chunks_parse_chunk(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx, ngx_buf_t *b);
static ngx_int_t ngx_http_file_chunks_send(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx, ngx_buf_t *b);
static void ngx_http_file_chunks_cleanup(void *data);
static ngx_buf_t *ngx_http_file_chunks_prepare_final_buffer(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx);
static char *ngx_http_file_chunks_xml_dtd(ngx_conf_t *cf, ngx_command_t *cmd,
        void *conf);


static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt   ngx_http_next_body_filter;

static ngx_str_t ngx_http_file_chunks_default_types[] = {
    ngx_string("text/xml"),
    ngx_string("application/xml"),
    ngx_null_string
};

static ngx_command_t ngx_http_file_chunks_filter_commands[] = {
    { ngx_string("file_chunks"),
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |
          NGX_HTTP_LIF_CONF | NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_file_chunks_conf_t, enable),
      NULL },

    { ngx_string("file_chunks_input_types"),
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF
          | NGX_CONF_1MORE,
      ngx_http_types_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_file_chunks_conf_t, input_type_keys),
      &ngx_http_file_chunks_default_types[0] },

    { ngx_string("file_chunks_xml_dtd"),
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF
          | NGX_CONF_TAKE1,
      ngx_http_file_chunks_xml_dtd,
      NGX_HTTP_LOC_CONF_OFFSET,
      0,
      NULL },

      ngx_null_command
};

static ngx_http_module_t ngx_http_file_chunks_filter_module_ctx = {
    ngx_http_file_chunks_filter_preconfigure, /* preconfiguration              */
    ngx_http_file_chunks_filter_init,         /* postconfiguration             */

    ngx_http_file_chunks_create_main_conf,    /* create main configuration     */
    NULL,                                     /* init main configuration       */

    NULL,                                     /* create server configuration   */
    NULL,                                     /* merge server configuration    */

    ngx_http_file_chunks_create_loc_conf,     /* create location configuration */
    ngx_http_file_chunks_merge_loc_conf       /* merge location configuration  */
};

ngx_module_t ngx_http_file_chunks_filter_module = {
    NGX_MODULE_V1,
    &ngx_http_file_chunks_filter_module_ctx, /* module context    */
    ngx_http_file_chunks_filter_commands,    /* module directives */
    NGX_HTTP_MODULE,                         /* module type       */
    NULL,                                    /* init master       */
    NULL,                                    /* init module       */
    NULL,                                    /* init process      */
    NULL,                                    /* init thread       */
    NULL,                                    /* exit thread       */
    ngx_http_file_chunks_filter_exit,        /* exit process      */
    ngx_http_file_chunks_filter_exit,        /* exit master       */
    NGX_MODULE_V1_PADDING
};


/* {{{ ngx_http_file_chunks_preconfiguration */
static ngx_int_t
ngx_http_file_chunks_filter_preconfigure(ngx_conf_t *cf)
{
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "file_chunks preconf");
    xmlInitParser();

    return NGX_OK;
}
/* }}} */


/* {{{ ngx_http_file_chunks_create_main_conf */
static void *
ngx_http_file_chunks_create_main_conf(ngx_conf_t *cf)
{
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "file_chunks create main conf");

    ngx_http_file_chunks_main_conf_t *conf;

    conf = ngx_palloc(cf->pool, sizeof(ngx_http_file_chunks_main_conf_t));
    if (conf == NULL) {
        return NULL;
    }

    if (ngx_array_init(&conf->dtd_files, cf->pool, 1,
                sizeof(ngx_http_file_chunks_main_conf_t)) != NGX_OK) {
        return NULL;
    }

    return conf;
}
/* }}} */


/* {{{ ngx_http_file_chunks_create_loc_conf */
static void *
ngx_http_file_chunks_create_loc_conf(ngx_conf_t *cf)
{
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "file_chunks create loc conf");

    ngx_http_file_chunks_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_chunks_conf_t));
    if (conf == NULL) {
        return NULL;
    }

    /*
     * set by ngx_pcalloc():
     *
     *     conf->input_types = { NULL };
     *     conf->input_type_keys = NULL;
     */

    conf->enable = NGX_CONF_UNSET;

    return conf;
}
/* }}} */


/* {{{ ngx_http_file_chunks_merge_loc_conf */
static char *
ngx_http_file_chunks_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_http_file_chunks_conf_t *prev = parent;
    ngx_http_file_chunks_conf_t *conf = child;

    ngx_conf_merge_value(conf->enable, prev->enable, 0);

    if (ngx_http_merge_types(cf, &conf->input_type_keys, &conf->input_types,
                &prev->input_type_keys, &prev->input_types,
                ngx_http_file_chunks_default_types) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    if (conf->dtd == NULL) {
        conf->dtd = prev->dtd;
    }

    return NGX_CONF_OK;
}
/* }}} */


/* {{{ ngx_http_file_chunks_header_filter */
static ngx_int_t
ngx_http_file_chunks_header_filter(ngx_http_request_t *r)
{
    ngx_http_file_chunks_ctx_t  *ctx;
    ngx_http_file_chunks_conf_t *conf;

    /* XXX replace with log_debug0 */
    ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "file_chunks header filter");

    if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED
            || (r->headers_out.status != NGX_HTTP_OK
                && r->headers_out.status != NGX_HTTP_FORBIDDEN
                && r->headers_out.status != NGX_HTTP_NOT_FOUND)
            || r->header_only) {
        return ngx_http_next_header_filter(r);
    }

    conf = ngx_http_get_module_loc_conf(r, ngx_http_file_chunks_filter_module);

    if (!conf->enable
            || ngx_http_test_content_type(r, &conf->input_types) == NULL) {
        return ngx_http_next_header_filter(r);
    }

    /* Check if module context already exists */
    ctx = ngx_http_get_module_ctx(r, ngx_http_file_chunks_filter_module);
    if (ctx) {
        return ngx_http_next_header_filter(r);
    }

    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_file_chunks_ctx_t));
    if (ctx == NULL) {
        return NGX_ERROR;
    }

    ngx_http_set_ctx(r, ctx, ngx_http_file_chunks_filter_module);

    ctx->request = r;

    r->main_filter_need_in_memory = 1;

    return ngx_http_next_header_filter(r);
}/* }}} */


/* {{{ ngx_http_file_chunks_body_filter */
static ngx_int_t
ngx_http_file_chunks_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
    int                         well_formed;
    ngx_chain_t                *cl;
    ngx_http_file_chunks_ctx_t *ctx;

    ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "file_chunks body filter");

    if (in == NULL) {
        return ngx_http_next_body_filter(r, in);
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_file_chunks_filter_module);
    if (ctx == NULL || ctx->done || r->header_only) {
        return ngx_http_next_body_filter(r, in);
    }

    for (cl = in; cl; cl = cl->next) {
        /* Parse next chunk of XML */
        if (ngx_http_file_chunks_parse_chunk(r, ctx, cl->buf) != NGX_OK) {
            if (ctx->ctxt->myDoc) {
                ctx->ctxt->myDoc->extSubset = NULL;
                xmlFreeDoc(ctx->ctxt->myDoc);
                ctx->ctxt->myDoc = NULL;
            }

            xmlFreeParserCtxt(ctx->ctxt);
            ctx->ctxt = NULL;

            return ngx_http_file_chunks_send(r, ctx, NULL);
        }

        /* No more XML to read in? */
        if (cl->buf->last_buf || cl->buf->last_in_chain) {
            well_formed = ctx->ctxt->wellFormed;
            ctx->doc = ctx->ctxt->myDoc;
            ctx->doc->extSubset = NULL;

            if (ctx->ctxt) {
                xmlFreeParserCtxt(ctx->ctxt);
                ctx->ctxt = NULL;
            }

            if (well_formed) {
                return ngx_http_file_chunks_send(r, ctx,
                        ngx_http_file_chunks_prepare_final_buffer(r, ctx));
            }

            if (ctx->doc) {
                xmlFreeDoc(ctx->doc);
                ctx->doc = NULL;
            }

            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                    "file_chunks: not well-formed XML");

            return ngx_http_file_chunks_send(r, ctx, NULL);
        }
    }

    return NGX_OK;
}
/* }}} */


/* {{{ ngx_http_file_chunks_filter_init */
static ngx_int_t
ngx_http_file_chunks_filter_init(ngx_conf_t *cf)
{
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "file_chunks init");

    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_file_chunks_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_file_chunks_body_filter;

    return NGX_OK;
}
/* }}} */


/* {{{ ngx_http_file_chunks_sax_external_subset */
static void
ngx_http_file_chunks_sax_external_subset(void *data, const xmlChar *name,
    const xmlChar *externalId, const xmlChar *systemId)
{
    xmlParserCtxtPtr ctxt = data;

    xmlDocPtr                    doc;
    xmlDtdPtr                    dtd;
    ngx_http_request_t          *r;
    ngx_http_file_chunks_ctx_t  *ctx;
    ngx_http_file_chunks_conf_t *conf;

    ctx = ctxt->sax->_private;
    r = ctx->request;

    conf = ngx_http_get_module_loc_conf(r, ngx_http_file_chunks_filter_module);

    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "file_chunks filter sax external subset: \"%s\" \"%s\" \"%s\"",
                   name ? name : (xmlChar *) "",
                   externalId ? externalId : (xmlChar *) "",
                   systemId ? systemId : (xmlChar *) "");

    doc = ctxt->myDoc;
    dtd = conf->dtd;
    doc->extSubset = dtd;
}
/* }}} */


/* {{{ ngx_http_file_chunks_sax_error */
static void ngx_cdecl
ngx_http_file_chunks_sax_error(void *data, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = data;

    size_t                      n;
    va_list                     args;
    ngx_http_file_chunks_ctx_t *ctx;
    u_char                      buf[NGX_MAX_ERROR_STR];

    ctx = ctxt->sax->_private;

    buf[0] = '\0';

    va_start(args, msg);
    n = (size_t) vsnprintf((char *) buf, NGX_MAX_ERROR_STR, msg, args);
    va_end(args);

    while (--n && (buf[n] == CR || buf[n] == LF)) { /* void */ }

    ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
                  "libxml2 error: \"%*s\"", n + 1, buf);
}
/* }}} */


/* {{{ ngx_http_file_chunks_parse_chunk */
static ngx_int_t
ngx_http_file_chunks_parse_chunk(ngx_http_request_t *r, ngx_http_file_chunks_ctx_t
        *ctx, ngx_buf_t *b)
{
    int               err;
    xmlParserCtxtPtr  ctxt;

    if (ctx->ctxt == NULL) {
        ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
        if (ctxt == NULL) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                    "xmlCreatePushParserCtxt() failed");
            return NGX_ERROR;
        }
        xmlCtxtUseOptions(ctxt, XML_PARSE_NOENT | XML_PARSE_DTDLOAD
                | XML_PARSE_NOWARNING);

        ctxt->sax->externalSubset     = ngx_http_file_chunks_sax_external_subset;
        ctxt->sax->setDocumentLocator = NULL;
        ctxt->sax->error              = ngx_http_file_chunks_sax_error;
        ctxt->sax->fatalError         = ngx_http_file_chunks_sax_error;
        ctxt->sax->_private           = ctx;

        ctx->ctxt    = ctxt;
        ctx->request = r;
    }

    err = xmlParseChunk(ctx->ctxt, (char *) b->pos, (int) (b->last - b->pos),
            (b->last_buf) || (b->last_in_chain));

    if (err == 0) {
        b->pos = b->last;
        return NGX_OK;
    }

    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
            "xmlParseChunk() failed, error:%d", err);

    return NGX_ERROR;
}
/* }}} */


/* {{{ ngx_http_file_chunks_cleanup */
static void
ngx_http_file_chunks_cleanup(void *data)
{
    ngx_free(data);
}
/* }}} */


/* {{{ ngx_http_file_chunks_cleanup_dtd */
static void
ngx_http_file_chunks_cleanup_dtd(void *data)
{
    xmlFreeDtd(data);
}
/* }}} */


/* {{{ ngx_http_file_chunks_send
 * Sends contents of buffer `b` to the body filter chain. */
static ngx_int_t
ngx_http_file_chunks_send(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx, ngx_buf_t *b)
{
    ngx_int_t           rc;
    ngx_chain_t         out;
    ngx_pool_cleanup_t *cln;

    ctx->done = 1;

    if (b == NULL) {
        return ngx_http_filter_finalize_request(r,
                &ngx_http_file_chunks_filter_module,
                NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        ngx_free(b->pos);
        return ngx_http_filter_finalize_request(r, &ngx_http_file_chunks_filter_module,
                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    if (r == r->main) {
        r->headers_out.content_length_n = b->last - b->pos;

        if (r->headers_out.content_length) {
            r->headers_out.content_length->hash = 0;
            r->headers_out.content_length = NULL;
        }

        ngx_http_clear_etag(r);
        ngx_http_clear_last_modified(r);
    }

    rc = ngx_http_next_header_filter(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        ngx_free(b->pos);
        return rc;
    }

    cln->handler = ngx_http_file_chunks_cleanup;
    cln->data = b->pos;

    out.buf = b;
    out.next = NULL;

    return ngx_http_next_body_filter(r, &out);
}
/* }}} */


/* {{{ ngx_http_file_chunks_prepare_final_buffer
 *  */
static ngx_buf_t *
ngx_http_file_chunks_prepare_final_buffer(ngx_http_request_t *r,
        ngx_http_file_chunks_ctx_t *ctx)
{
    int                          doc_type;
    size_t                       len, n;
    ngx_file_t                   file;
    ngx_file_info_t              fi;
    ngx_err_t                    err;
    u_char                      *type = NULL , *encoding = NULL;
    ngx_buf_t                   *b = NULL;
    u_char                      *buf = NULL;
    xmlDocPtr                    doc;

    doc = ctx->doc;
    doc_type = doc->type;

    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "file_chunks filter encoding:%s doctype:%d",
                   encoding ? encoding : (u_char *) "(null)",
                   doc_type);

    /* === TEST BLOCK START === */

    /* XXX We have XML tree parsed within `doc`. Now we have to obtain
     * corresponding file from the links inside the XML, and put the file
     * contents into `buf`*/

    /* XXX Use ngx_file_t, ngx_read_file() */

    u_char filename[] = "/home/ruslan/projects/nginx/modules/file_chunks_filter/response.xml";
    ngx_str_t name = {sizeof(filename) - 1, filename};

    ngx_memzero(&file, sizeof(ngx_file_t));
    file.name = name;
    file.log = r->connection->log;
    file.fd = ngx_open_file(name.data, NGX_FILE_RDONLY, 0, 0);
    if (file.fd == NGX_INVALID_FILE) {
        err = ngx_errno;
        if (err != NGX_ENOENT) {
            ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
                               ngx_open_file_n " \"%s\" failed", name.data);
        }

        xmlFreeDoc(doc);
        return NULL;
    }

    if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
                           ngx_fd_info_n " \"%s\" failed", name.data);

        goto ret;
    }

    len = (size_t) ngx_file_size(&fi);


    buf = ngx_palloc(r->pool, len);
    if (buf == NULL) {
        goto ret;
    }

    n = ngx_read_file(&file, buf, len, 0);

    if (n == (size_t) NGX_ERROR) {
        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
                           ngx_read_file_n " \"%s\" failed", name.data);
        goto ret;
    }

    if ((size_t) n != len) {
        ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
            ngx_read_file_n " \"%s\" returned only %z bytes instead of %z",
            name.data, n, len);
        goto ret;
    }
    /* === TEST BLOCK END === */

    xmlFreeDoc(doc);
    doc = NULL;

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        goto ret;
    }

    b->pos = buf;
    b->last = buf + len;
    b->memory = 1;

    /* XXX */
    if (encoding) {
        r->headers_out.charset.len = ngx_strlen(encoding);
        r->headers_out.charset.data = encoding;
    }

    if (r != r->main) {
        return b;
    }

    b->last_buf = 1;

    if (type) {
        len = ngx_strlen(type);

        r->headers_out.content_type_len = len;
        r->headers_out.content_type.data = type;
    } else {
        r->headers_out.content_type_len = sizeof("text/plain") - 1;
        ngx_str_set(&r->headers_out.content_type, "text/plain");
    }

    r->headers_out.content_type_lowcase = NULL;

ret:
    if (buf) {
        ngx_free(buf);
    }

    if (doc) {
        xmlFreeDoc(doc);
    }

    if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
                      ngx_close_file_n " \"%s\" failed", name.data);
    }

    return b;
}
/* }}} */


/* {{{ ngx_http_file_chunks_xml_dtd */
static char *
ngx_http_file_chunks_xml_dtd(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_file_chunks_conf_t *fccf = conf;

    ngx_str_t                        *value;
    ngx_uint_t                        i;
    ngx_pool_cleanup_t               *cln;
    ngx_http_file_chunks_file_t      *file;
    ngx_http_file_chunks_main_conf_t *fmcf;

    if (fccf->dtd) {
        return "is duplicate";
    }

    value = cf->args->elts;

    fmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_file_chunks_filter_module);

    file = fmcf->dtd_files.elts;
    for (i = 0; i < fmcf->dtd_files.nelts; i++) {
        if (ngx_strcmp(file[i].name, value[1].data) == 0) {
            fccf->dtd = file[i].data;
            return NGX_CONF_OK;
        }
    }

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        return NGX_CONF_ERROR;
    }

    fccf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data);
    if (fccf->dtd == NULL) {
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "file_chunks xmlParseDTD() failed");
        return NGX_CONF_ERROR;
    }

    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "file_chunks parsed DTD %s", value[1].data);

    cln->handler = ngx_http_file_chunks_cleanup_dtd;
    cln->data = fccf->dtd;

    file = ngx_array_push(&fmcf->dtd_files);
    if (file == NULL) {
        return NGX_CONF_ERROR;
    }

    file->name = value[1].data;
    file->data = fccf->dtd;

    return NGX_CONF_OK;
}
/* }}} */


/* {{{ ngx_http_file_chunks_filter_exit */
static void
ngx_http_file_chunks_filter_exit(ngx_cycle_t *cycle)
{
    xmlCleanupParser();
}
/* }}} */


/* vim: et ts=4 sts=4 sw=4 fdm=marker tw=80
 * vim: path+=/usr/include/libxml2
 */

This paste never expires. View raw. Pasted through import.