Mon, 10 Jul 2023 18:52:28 +0200
add WS_CFLAGS to admin makefile
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 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
31 | #include <cx/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 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
35 | // macro for creating an error string (ucx cxmutstr) |
44
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 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
38 | static cxmutstr error_400 = ERRMSG("<html><body>bad request</body></html>"); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
39 | static cxmutstr error_403 = ERRMSG("<html><body>forbidden</body></html>"); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
40 | static cxmutstr error_404 = ERRMSG("<html><body>not found</body></html>"); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
41 | static cxmutstr error_500 = ERRMSG("<html><body>server error</body></html>"); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
42 | static cxmutstr 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 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
44 | static cxmutstr error_std = ERRMSG("<html><body>error</body></html>"); |
44
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; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
48 | cxmutstr msg; |
477
39ebd50cfc12
fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
49 | if(status <= 0) { |
39ebd50cfc12
fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
50 | status = 500; |
39ebd50cfc12
fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
51 | msg = error_500; |
39ebd50cfc12
fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
52 | } else if(status < 400) { |
137
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
53 | msg.ptr = NULL; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
54 | msg.length = 0; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
55 | } else { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
56 | switch(status) { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
57 | default: msg = error_std; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
58 | case 403: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
59 | msg = error_403; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
60 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
61 | } |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
62 | case 404: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
63 | msg = error_404; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
64 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
65 | } |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
66 | case 500: { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
67 | msg = error_500; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
68 | break; |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
69 | } |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | } |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | } |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | |
132
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
73 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
74 | pblock_removekey(pb_key_content_length, rq->srvhdrs); |
e9afb5387007
fixes broken error response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
75 | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | pblock_kninsert(pb_key_content_length, msg.length, rq->srvhdrs); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | pblock_nvinsert("content-type", "text/html", rq->srvhdrs); |
477
39ebd50cfc12
fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
78 | protocol_status(sn, rq, status, NULL); |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | http_start_response(sn, rq); |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | |
137
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
81 | if(msg.length > 0) { |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
82 | net_write(sn->csd, msg.ptr, msg.length); |
ca0cf1016a8b
adds redirect saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
132
diff
changeset
|
83 | } |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | return REQ_PROCEED; |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | } |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
87 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
88 | void fatal_error(HTTPRequest *req, int status) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
89 | cxmutstr msg = error_500; |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
90 | char *statusmsg = "Internal Server Error"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
91 | switch(status) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
92 | case 400: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
93 | msg = error_400; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
94 | statusmsg = "Bad request"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
95 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
96 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
97 | case 500: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
98 | msg = error_500; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
99 | statusmsg = "Internal Server Error"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
100 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
101 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
102 | case 503: { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
103 | msg = error_503; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
104 | statusmsg = "Service Unavailable"; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
105 | break; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
106 | } |
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 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
109 | char buf[512]; |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
110 | 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
|
111 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
112 | // set socket blocking |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
113 | int flags; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
114 | 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
|
115 | flags = 0; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
116 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
117 | 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
|
118 | // error |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
119 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
120 | |
108 | 121 | req->connection->write(req->connection, buf, len); |
122 | 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
|
123 | } |