src/server/daemon/error.c

Mon, 26 Dec 2016 16:46:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 26 Dec 2016 16:46:55 +0100
changeset 129
fd324464f56f
parent 108
2a394ccdd778
child 132
e9afb5387007
permissions
-rw-r--r--

adds support for ssl cert chain files and improves ssl error handling

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;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 sstr_t msg = error_std;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 switch(status) {
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 case 403: {
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 msg = error_403;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 break;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 case 404: {
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 msg = error_404;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 break;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 case 500: {
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 msg = error_500;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 break;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 }
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 pblock_kninsert(pb_key_content_length, msg.length, rq->srvhdrs);
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 pblock_removekey(pb_key_content_type, rq->srvhdrs);
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 pblock_nvinsert("content-type", "text/html", rq->srvhdrs);
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 http_start_response(sn, rq);
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 net_write(sn->csd, msg.ptr, msg.length);
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 return REQ_PROCEED;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
73
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
74 void fatal_error(HTTPRequest *req, int status) {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
75 sstr_t msg = error_500;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
76 char *statusmsg = "Internal Server Error";
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
77 switch(status) {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
78 case 400: {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
79 msg = error_400;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
80 statusmsg = "Bad request";
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
81 break;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
82 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
83 case 500: {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
84 msg = error_500;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
85 statusmsg = "Internal Server Error";
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
86 break;
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 case 503: {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
89 msg = error_503;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
90 statusmsg = "Service Unavailable";
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 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
94
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
95 char buf[512];
70
4e6e812c1d97 fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
96 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
97
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
98 // set socket blocking
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
99 int flags;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
100 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
101 flags = 0;
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 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
104 // error
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
105 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
106
108
2a394ccdd778 fixed ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 92
diff changeset
107 req->connection->write(req->connection, buf, len);
2a394ccdd778 fixed ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 92
diff changeset
108 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
109
108
2a394ccdd778 fixed ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 92
diff changeset
110 req->connection->close(req->connection);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
111 }

mercurial