xref: /unit/src/nxt_http_response.c (revision 431:5817734dd9b9)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_router.h>
8 #include <nxt_http.h>
9 
10 
11 static nxt_int_t nxt_http_response_status(void *ctx, nxt_http_field_t *field,
12     uintptr_t data);
13 static nxt_int_t nxt_http_response_skip(void *ctx, nxt_http_field_t *field,
14     uintptr_t data);
15 static nxt_int_t nxt_http_response_field(void *ctx, nxt_http_field_t *field,
16     uintptr_t offset);
17 
18 
19 nxt_lvlhsh_t  nxt_response_fields_hash;
20 
21 static nxt_http_field_proc_t   nxt_response_fields[] = {
22     { nxt_string("Status"),         &nxt_http_response_status, 0 },
23     { nxt_string("Server"),         &nxt_http_response_skip, 0 },
24     { nxt_string("Connection"),     &nxt_http_response_skip, 0 },
25     { nxt_string("Content-Type"),   &nxt_http_response_field,
26         offsetof(nxt_http_request_t, resp.content_type) },
27     { nxt_string("Content-Length"), &nxt_http_response_field,
28         offsetof(nxt_http_request_t, resp.content_length) },
29 };
30 
31 
32 nxt_int_t
33 nxt_http_response_hash_init(nxt_task_t *task, nxt_runtime_t *rt)
34 {
35     return nxt_http_fields_hash(&nxt_response_fields_hash, rt->mem_pool,
36                     nxt_response_fields, nxt_nitems(nxt_response_fields));
37 }
38 
39 
40 nxt_int_t
41 nxt_http_response_status(void *ctx, nxt_http_field_t *field,
42     uintptr_t data)
43 {
44     nxt_int_t           status;
45     nxt_http_request_t  *r;
46 
47     r = ctx;
48 
49     field->skip = 1;
50 
51     if (field->value_length >= 3) {
52         status = nxt_int_parse(field->value, 3);
53 
54         if (status >= 100 && status <= 999) {
55             r->status = status;
56             return NXT_OK;
57         }
58     }
59 
60     return NXT_ERROR;
61 }
62 
63 
64 nxt_int_t
65 nxt_http_response_skip(void *ctx, nxt_http_field_t *field, uintptr_t data)
66 {
67     field->skip = 1;
68 
69     return NXT_OK;
70 }
71 
72 
73 nxt_int_t
74 nxt_http_response_field(void *ctx, nxt_http_field_t *field, uintptr_t offset)
75 {
76     nxt_http_request_t  *r;
77 
78     r = ctx;
79 
80     nxt_value_at(nxt_http_field_t *, r, offset) = field;
81 
82     return NXT_OK;
83 }
84