]>
Commit | Line | Data |
---|---|---|
582311d7 | 1 | /* |
0789c7d8 | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
582311d7 DDO |
3 | * |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #ifndef OSSL_HTTP_SERVER_H | |
11 | # define OSSL_HTTP_SERVER_H | |
12 | ||
13 | # include "apps.h" | |
14 | ||
15 | # ifndef HAVE_FORK | |
16 | # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) | |
17 | # define HAVE_FORK 0 | |
18 | # else | |
19 | # define HAVE_FORK 1 | |
20 | # endif | |
21 | # endif | |
22 | ||
23 | # if HAVE_FORK | |
24 | # undef NO_FORK | |
25 | # else | |
26 | # define NO_FORK | |
27 | # endif | |
28 | ||
29 | # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \ | |
30 | && !defined(OPENSSL_NO_POSIX_IO) | |
31 | # define HTTP_DAEMON | |
32 | # include <sys/types.h> | |
33 | # include <sys/wait.h> | |
34 | # include <syslog.h> | |
35 | # include <signal.h> | |
36 | # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */ | |
582311d7 DDO |
37 | # endif |
38 | ||
4599ea9f DDO |
39 | # undef LOG_TRACE |
40 | # undef LOG_DEBUG | |
41 | # undef LOG_INFO | |
42 | # undef LOG_WARNING | |
43 | # undef LOG_ERR | |
44 | # define LOG_TRACE 8 | |
45 | # define LOG_DEBUG 7 | |
46 | # define LOG_INFO 6 | |
47 | # define LOG_WARNING 4 | |
48 | # define LOG_ERR 3 | |
49 | ||
582311d7 DDO |
50 | /*- |
51 | * Log a message to syslog if multi-threaded HTTP_DAEMON, else to bio_err | |
52 | * prog: the name of the current app | |
53 | * level: the severity of the message, e.g., LOG_ERR | |
54 | * fmt: message with potential extra parameters like with printf() | |
55 | * returns nothing | |
56 | */ | |
57 | void log_message(const char *prog, int level, const char *fmt, ...); | |
58 | ||
59 | # ifndef OPENSSL_NO_SOCK | |
60 | /*- | |
4599ea9f | 61 | * Initialize an HTTP server, setting up its listening BIO |
582311d7 DDO |
62 | * prog: the name of the current app |
63 | * port: the port to listen on | |
4599ea9f | 64 | * verbosity: the level of verbosity to use, or -1 for default: LOG_INFO |
582311d7 DDO |
65 | * returns a BIO for accepting requests, NULL on error |
66 | */ | |
4599ea9f | 67 | BIO *http_server_init(const char *prog, const char *port, int verbosity); |
5a2ba207 | 68 | |
582311d7 DDO |
69 | /*- |
70 | * Accept an ASN.1-formatted HTTP request | |
71 | * it: the expected request ASN.1 type | |
72 | * preq: pointer to variable where to place the parsed request | |
5a2ba207 | 73 | * ppath: pointer to variable where to place the request path, or NULL |
19f97fe6 | 74 | * pcbio: pointer to variable where to place the BIO for sending the response to |
582311d7 | 75 | * acbio: the listening bio (typically as returned by http_server_init_bio()) |
19f97fe6 DDO |
76 | * found_keep_alive: for returning flag if client requests persistent connection |
77 | * prog: the name of the current app, for diagnostics only | |
5a2ba207 | 78 | * accept_get: whether to accept GET requests (in addition to POST requests) |
582311d7 | 79 | * timeout: connection timeout (in seconds), or 0 for none/infinite |
5a2ba207 DDO |
80 | * returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL |
81 | * returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL | |
82 | * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while | |
83 | * *ppath == NULL and *preq == NULL if and only if the request is invalid, | |
84 | * On return value 1 the caller is responsible for sending an HTTP response, | |
85 | * using http_server_send_asn1_resp() or http_server_send_status(). | |
86 | * The caller must free any non-NULL *preq, *ppath, and *pcbio pointers. | |
582311d7 DDO |
87 | */ |
88 | int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, | |
5a2ba207 | 89 | char **ppath, BIO **pcbio, BIO *acbio, |
19f97fe6 | 90 | int *found_keep_alive, |
4599ea9f | 91 | const char *prog, int accept_get, int timeout); |
5a2ba207 | 92 | |
582311d7 DDO |
93 | /*- |
94 | * Send an ASN.1-formatted HTTP response | |
95 | * cbio: destination BIO (typically as returned by http_server_get_asn1_req()) | |
96 | * note: cbio should not do an encoding that changes the output length | |
e304aa87 | 97 | * keep_alive: grant persistent connection |
582311d7 DDO |
98 | * content_type: string identifying the type of the response |
99 | * it: the response ASN.1 type | |
582311d7 DDO |
100 | * resp: the response to send |
101 | * returns 1 on success, 0 on failure | |
102 | */ | |
19f97fe6 DDO |
103 | int http_server_send_asn1_resp(BIO *cbio, int keep_alive, |
104 | const char *content_type, | |
582311d7 | 105 | const ASN1_ITEM *it, const ASN1_VALUE *resp); |
5a2ba207 DDO |
106 | |
107 | /*- | |
108 | * Send a trivial HTTP response, typically to report an error or OK | |
109 | * cbio: destination BIO (typically as returned by http_server_get_asn1_req()) | |
110 | * status: the status code to send | |
111 | * reason: the corresponding human-readable string | |
112 | * returns 1 on success, 0 on failure | |
113 | */ | |
114 | int http_server_send_status(BIO *cbio, int status, const char *reason); | |
115 | ||
582311d7 DDO |
116 | # endif |
117 | ||
118 | # ifdef HTTP_DAEMON | |
119 | extern int multi; | |
120 | extern int acfd; | |
121 | ||
122 | void socket_timeout(int signum); | |
123 | void spawn_loop(const char *prog); | |
124 | # endif | |
125 | ||
126 | #endif |