Wed, 28 Dec 2016 12:23:51 +0100
using host header to select virtual server
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
3da1f7b6847f
added some error messages
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 |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "error.h" |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | |
92
382bff43c6eb
fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
70
diff
changeset
|
31 | #include <ucx/string.h> |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include "../util/pblock.h" |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
33 | #include "../util/strbuf.h" |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | // macro for creating an error string (ucx sstr_t) |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #define ERRMSG(s) { s, sizeof(s)-1 } |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
38 | static sstr_t error_400 = ERRMSG("<html><body>bad request</body></html>"); |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | static sstr_t error_403 = ERRMSG("<html><body>forbidden</body></html>"); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | static sstr_t error_404 = ERRMSG("<html><body>not found</body></html>"); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | static sstr_t error_500 = ERRMSG("<html><body>server error</body></html>"); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
42 | static sstr_t error_503 = ERRMSG("<html><body>service unavailable</body></html>"); |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | static sstr_t error_std = ERRMSG("<html><body>error</body></html>"); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | int nsapi_error_request(Session *sn, Request *rq) { |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | short status = rq->status_num; |
137
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
48 | sstr_t msg; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
49 | if(status < 400) { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
50 | msg.ptr = NULL; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
51 | msg.length = 0; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
52 | } else { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
53 | switch(status) { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
54 | default: msg = error_std; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
55 | case 403: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
56 | msg = error_403; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
57 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
58 | } |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
59 | case 404: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
60 | msg = error_404; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
61 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
62 | } |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
63 | case 500: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
64 | msg = error_500; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
65 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
66 | } |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | } |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | } |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | |
132
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
70 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
71 | pblock_removekey(pb_key_content_length, rq->srvhdrs); |
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
72 | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | pblock_kninsert(pb_key_content_length, msg.length, rq->srvhdrs); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | pblock_nvinsert("content-type", "text/html", rq->srvhdrs); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | http_start_response(sn, rq); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | |
137
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
77 | if(msg.length > 0) { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
78 | net_write(sn->csd, msg.ptr, msg.length); |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
79 | } |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | return REQ_PROCEED; |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | } |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
83 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
84 | void fatal_error(HTTPRequest *req, int status) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
85 | sstr_t msg = error_500; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
86 | char *statusmsg = "Internal Server Error"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
87 | switch(status) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
88 | case 400: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
89 | msg = error_400; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
90 | statusmsg = "Bad request"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
91 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
92 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
93 | case 500: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
94 | msg = error_500; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
95 | statusmsg = "Internal Server Error"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
96 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
97 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
98 | case 503: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
99 | msg = error_503; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
100 | statusmsg = "Service Unavailable"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
101 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
102 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
103 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
104 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
105 | char buf[512]; |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
106 | int len = snprintf(buf, 512, "HTTP/1.1 %d %s\r\nServer: webserver\r\nContent-type: text/html\r\nContent-length: %zd\r\n\r\n", status, statusmsg, msg.length); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
107 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
108 | // set socket blocking |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
109 | int flags; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
110 | if (-1 == (flags = fcntl(req->connection->fd, F_GETFL, 0))) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
111 | flags = 0; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
112 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
113 | if (fcntl(req->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
114 | // error |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
115 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
116 | |
108 | 117 | req->connection->write(req->connection, buf, len); |
118 | req->connection->write(req->connection, msg.ptr, msg.length); | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
119 | |
108 | 120 | req->connection->close(req->connection); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
121 | } |