src/server/daemon/protocol.c

Tue, 10 Nov 2015 21:18:51 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 10 Nov 2015 21:18:51 +0100
changeset 112
b962d83124bc
parent 110
43a746e905f6
child 121
a881dc866e23
permissions
-rw-r--r--

merge

4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "protocol.h"
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
31 #include "../util/pblock.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
32 #include "../util/pool.h"
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "session.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
34 #include "../util/io.h"
103
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
35 #include "../util/util.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
36 #include "../util/strbuf.h"
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 void protocol_status(Session *sn, Request *rq, int n, const char *m) {
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 rq->status_num = n;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 const char *msg = m ? m : protocol_status_message(n);
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 pb_param *pp = pblock_removekey(pb_key_status, rq->srvhdrs);
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 if (pp != NULL) {
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 param_free(pp);
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 pp = pblock_key_param_create(rq->srvhdrs, pb_key_status, msg, strlen(msg));
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 pblock_kpinsert(pb_key_status, pp, rq->srvhdrs);
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 /*
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 * http_status_message from Open Webserver (frame/http.cpp)
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 * TODO: replace, use sstr_t
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 NSAPI_PUBLIC const char * protocol_status_message (int code)
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 {
87
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
60 WS_ASSERT(code > 0);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
61
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 const char *r;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 switch (code)
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 {
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 case PROTOCOL_CONTINUE : // 100
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 r = "Continue";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 case PROTOCOL_SWITCHING: //101
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 r = "Switching Protocols";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 case PROTOCOL_OK: // 200
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 r = "OK";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 case PROTOCOL_CREATED: // 201
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 r = "Created";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 case PROTOCOL_ACCEPTED: // 202
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 r = "Accepted";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 case PROTOCOL_NONAUTHORITATIVE: // 203
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 r = "Non-Authoritative Information";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 case PROTOCOL_NO_CONTENT: //204
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 /* There is another define to PROTOCOL_NO_RESPONSE for 204 in nsapi.h
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 The spec maps this to No Content.
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 Hence cahnging this to No Content
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 r = "No Content";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 case PROTOCOL_RESET_CONTENT: // 205
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 r = "Reset Content";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 case PROTOCOL_PARTIAL_CONTENT: // 206
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 r = "Partial Content";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 case PROTOCOL_MULTI_STATUS: // 207
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 r = "Multi Status";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 case PROTOCOL_MULTIPLE_CHOICES: // 300
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 r = "Multiple Choices";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 case PROTOCOL_MOVED_PERMANENTLY: // 301
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 r = "Moved Permanently";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 case PROTOCOL_REDIRECT: // 302
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 r = "Moved Temporarily"; /* The spec actually says "Found" */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 case PROTOCOL_SEE_OTHER: // 303
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 r = "See Other";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 case PROTOCOL_NOT_MODIFIED: // 304
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 r = "Use local copy"; /* The spec actually says "Not Modified" */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 case PROTOCOL_USE_PROXY: // 305
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 r = "Use Proxy";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 case PROTOCOL_TEMPORARY_REDIRECT: // 307
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 r = "Temporary Redirect";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 case PROTOCOL_BAD_REQUEST: // 400
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 r = "Bad request";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 case PROTOCOL_UNAUTHORIZED: // 401
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 r = "Unauthorized";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 case PROTOCOL_PAYMENT_REQUIRED: // 402
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 r = "Payment Required";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 case PROTOCOL_FORBIDDEN: // 403
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 r = "Forbidden";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 case PROTOCOL_NOT_FOUND: // 404
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 r = "Not found";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 case PROTOCOL_METHOD_NOT_ALLOWED: // 405 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 r = "Method Not Allowed";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 case PROTOCOL_NOT_ACCEPTABLE: // 406 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 r = "Not Acceptable";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 case PROTOCOL_PROXY_UNAUTHORIZED: // 407
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 r = "Proxy Authentication Required";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 case PROTOCOL_REQUEST_TIMEOUT: // 408 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 r = "Request Timeout";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 case PROTOCOL_CONFLICT: // 409
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 r = "Conflict"; /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 case PROTOCOL_GONE: // 410
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 r = "Gone"; /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 case PROTOCOL_LENGTH_REQUIRED: // 411 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 r = "Length Required";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 case PROTOCOL_PRECONDITION_FAIL: // 412 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 r = "Precondition Failed";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 case PROTOCOL_ENTITY_TOO_LARGE: // 413 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 r = "Request Entity Too Large";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 case PROTOCOL_URI_TOO_LARGE: // 414 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 r = "Request-URI Too Large";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 case PROTOCOL_UNSUPPORTED_MEDIA_TYPE: // 415
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 r = "Unsupported Media Type";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 case PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE: // 416
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 r = "Requested range not satisfiable";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 case PROTOCOL_EXPECTATION_FAILED: // 417
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 r = "Expectation Failed";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 case PROTOCOL_LOCKED: // 423
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 r = "Locked";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 case PROTOCOL_FAILED_DEPENDENCY: // 424
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 r = "Failed Dependency";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 case PROTOCOL_SERVER_ERROR: // 500
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 r = "Server Error"; /* The spec actually says "Internal Server Error" */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 case PROTOCOL_NOT_IMPLEMENTED: // 501
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 r = "Not Implemented";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 case PROTOCOL_BAD_GATEWAY: // 502
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 r = "Bad Gateway";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 case PROTOCOL_SERVICE_UNAVAILABLE: // 503
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 r = "Service Unavailable";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 case PROTOCOL_GATEWAY_TIMEOUT: // 504 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 r = "Gateway Timeout";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 case PROTOCOL_VERSION_NOT_SUPPORTED: // 505 /* HTTP/1.1 */
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 r = "HTTP Version Not Supported";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 case PROTOCOL_INSUFFICIENT_STORAGE: // 507
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 r = "Insufficient Storage";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 default:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 switch (code / 100)
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 {
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 case 1:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 r = "Information";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 case 2:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 r = "Success";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 case 3:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 r = "Redirect";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 case 4:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 r = "Client error";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 case 5:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 r = "Server error";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 default:
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 r = "Unknown reason";
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 break;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 return r;
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
231 void add_http_status_line(sbuf_t *out, pool_handle_t *pool, Request *rq) {
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
232 sbuf_write(out, "HTTP/1.1 ", 9);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
234 char *status_code_str = pool_malloc(pool, 8);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 int sc_len = snprintf(status_code_str, 8, "%d ", rq->status_num);
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
236 sbuf_write(out, status_code_str, sc_len);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 char *scmsg = pblock_findkeyval(pb_key_status, rq->srvhdrs);
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
239 if(scmsg == NULL) {
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
240 scmsg = "OK";
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
241 }
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
242 sbuf_write(out, scmsg, strlen(scmsg));
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
244 sbuf_write(out, "\r\n", 2);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
247 void add_http_response_header(sbuf_t *out, Request *rq) {
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
248 pblock *h = rq->srvhdrs;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
249 pb_entry *p;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
250
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
251 for(int i=0;i<h->hsize;i++) {
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
252 p = h->ht[i];
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
253 while(p != NULL) {
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
254 /* from http.cpp */
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
255 const pb_key *key = PARAM_KEY(p->param);
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
256 if (key == pb_key_status || key == pb_key_server || key == pb_key_date) {
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
257 /* Skip internal Status:, Server:, and Date: information */
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
258 p = p->next;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
259 continue;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
260 }
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
261 /* end http.cpp */
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
262
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
263 char *name = p->param->name;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
264 char *value = p->param->value;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
265
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
266 // make first char of name uppercase
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
267 size_t name_len = strlen(name);
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
268 if(name[0] > 90) {
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
269 /*
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
270 * make first char uppercase and write the remaining chars
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
271 * unmodified to the buffer
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
272 */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
273 sbuf_put(out, name[0]-32);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
274 if(name_len > 1) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
275 sbuf_write(out, name+1, name_len-1);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
276 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
277 } else {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
278 // first char is already uppercase so just write the name
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
279 sbuf_write(out, name, name_len);
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
280 }
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
281
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
282 sbuf_write(out, ": ", 2);
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
283 sbuf_write(out, value, strlen(value));
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
284 sbuf_write(out, "\r\n", 2);
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
285
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
286 p = p->next;
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
287 }
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
288 }
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
289 }
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
290
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 int http_start_response(Session *sn, Request *rq) {
108
2a394ccdd778 fixed ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
292 Connection *conn = ((NSAPISession*)sn)->connection;
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
294 if(rq->status_num == -1) {
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
295 protocol_status(sn, rq, 200, "OK");
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
296 }
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
297
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
298 // set socket blocking
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
299 //int flags;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
300 //flags = fcntl(fd, F_GETFL, 0);
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
301 //fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
303 // output buffer
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
304 sbuf_t *out = sbuf_new(512);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
306 // add the http status line to the output buffer
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
307 add_http_status_line(out, sn->pool, rq);
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
308
103
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
309 // add date header
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
310 struct tm mtms;
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
311 struct tm *mtm = system_gmtime(&rq->req_start, &mtms);
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
312 char date[HTTP_DATE_LEN + 1];
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
313 strftime(date, HTTP_DATE_LEN, HTTP_DATE_FMT, mtm);
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
314 sbuf_write(out, "Date: ", 6);
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
315 sbuf_write(out, date, strlen(date));
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
316 sbuf_write(out, "\r\n", 2);
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
317
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
318 // add server header
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
319 sbuf_write(out, "Server: webserver\r\n", 19);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
320
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
321 // check content length ans transfer encoding
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
322 char *ctlen = pblock_findkeyval(pb_key_content_length, rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
323 char *enc = pblock_findkeyval(pb_key_transfer_encoding, rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
324 if(ctlen && enc) {
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
325 pblock_removekey(pb_key_transfer_encoding, rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
326 }
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
327 if(!ctlen) {
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
328 // set transfer-encoding header
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
329 if(!enc) {
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
330 pblock_kvinsert(
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
331 pb_key_transfer_encoding,
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
332 "chunked",
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
333 7,
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
334 rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
335 } else if(strcmp(enc, "chunked")) {
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
336 pblock_removekey(pb_key_transfer_encoding, rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
337 pblock_kvinsert(
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
338 pb_key_transfer_encoding,
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
339 "chunked",
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
340 7,
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
341 rq->srvhdrs);
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
342 }
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
343
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
344 // set stream property
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
345 HttpStream *stream = (HttpStream*)sn->csd;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
346 stream->chunked_enc = 1;
80
0de4a90979e1 fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
347 rq->rq_attr.chunked = 1;
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
348 }
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
349
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
350 // add header from rq->srvhdrs
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
351 add_http_response_header(out, rq);
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
352
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
353 // add connection header
101
7fbcdbad0baa added support for absolute URIs and improved keep alive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
354 if(rq->rq_attr.keep_alive) {
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
355 sbuf_write(out, "Connection: keep-alive\r\n", 24);
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
356 pblock_kvinsert(pb_key_connection, "keep-alive", 10, rq->srvhdrs);
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
357 } else {
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
358 sbuf_write(out, "Connection: close\r\n", 19);
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
359 pblock_kvinsert(pb_key_connection, "close", 5, rq->srvhdrs);
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
360 }
101
7fbcdbad0baa added support for absolute URIs and improved keep alive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
361
7
3c2ed7a7a5fd request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
362
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
363 // response header end
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
364 sbuf_write(out, "\r\n", 2);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365
65
14722c5f8856 added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
366 // flush buffer to the socket
108
2a394ccdd778 fixed ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
367 conn->write(conn, out->ptr, out->length);
11
24d804a2799f Fixed response header bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
368 sbuf_free(out);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 rq->senthdrs = 1;
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
371
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
372 return 0;
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 }
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374
9
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
375 int request_header(char *name, char **value, Session *sn, Request *rq) {
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
376 const pb_key *key = pblock_key(name);
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
377 pb_param *pp = pblock_findkey(key, rq->headers);
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
378 if(pp != NULL) {
21
627b09ee74e4 New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
379 *value = pp->value;
9
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
380 return REQ_PROCEED;
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
381 } else {
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
382 //return REQ_ABORTED;
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
383 *value = NULL;
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
384 return REQ_NOACTION;
9
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
385 }
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
386 }
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
387
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
388 char *http_uri2url(const char *prefix, const char *suffix) {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
389 // TODO: implement
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
390 return NULL;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
391 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
392
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
393 char *http_uri2url_dynamic(const char *prefix, const char *suffix,
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
394 Session *sn, Request *rq)
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
395 {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
396 // TODO: implement
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
397 return NULL;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
398 }

mercurial