xref: /openssl/apps/include/http_server.h (revision 8a2ec00d)
1 /*
2  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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 # include "log.h"
15 
16 # ifndef HAVE_FORK
17 #  if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
18 #   define HAVE_FORK 0
19 #  else
20 #   define HAVE_FORK 1
21 #  endif
22 # endif
23 
24 # if HAVE_FORK
25 #  undef NO_FORK
26 # else
27 #  define NO_FORK
28 # endif
29 
30 # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
31     && !defined(OPENSSL_NO_POSIX_IO)
32 #  define HTTP_DAEMON
33 #  include <sys/types.h>
34 #  include <sys/wait.h>
35 #  include <signal.h>
36 #  define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
37 # endif
38 
39 # ifndef OPENSSL_NO_SOCK
40 /*-
41  * Initialize an HTTP server, setting up its listening BIO
42  * prog: the name of the current app
43  * port: the port to listen on
44  * verbosity: the level of verbosity to use, or -1 for default: LOG_INFO
45  * returns a BIO for accepting requests, NULL on error
46  */
47 BIO *http_server_init(const char *prog, const char *port, int verbosity);
48 
49 /*-
50  * Accept an ASN.1-formatted HTTP request
51  * it: the expected request ASN.1 type
52  * preq: pointer to variable where to place the parsed request
53  * ppath: pointer to variable where to place the request path, or NULL
54  * pcbio: pointer to variable where to place the BIO for sending the response to
55  * acbio: the listening bio (typically as returned by http_server_init_bio())
56  * found_keep_alive: for returning flag if client requests persistent connection
57  * prog: the name of the current app, for diagnostics only
58  * accept_get: whether to accept GET requests (in addition to POST requests)
59  * timeout: connection timeout (in seconds), or 0 for none/infinite
60  * returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL
61  * returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL
62  * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while
63  * *ppath == NULL and *preq == NULL if and only if the request is invalid,
64  * On return value 1 the caller is responsible for sending an HTTP response,
65  * using http_server_send_asn1_resp() or http_server_send_status().
66  * The caller must free any non-NULL *preq, *ppath, and *pcbio pointers.
67  */
68 int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
69                              char **ppath, BIO **pcbio, BIO *acbio,
70                              int *found_keep_alive,
71                              const char *prog, int accept_get, int timeout);
72 
73 /*-
74  * Send an ASN.1-formatted HTTP response
75  * prog: the name of the current app, for diagnostics only
76  * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
77  *       note: cbio should not do an encoding that changes the output length
78  * keep_alive: grant persistent connection
79  * content_type: string identifying the type of the response
80  * it: the response ASN.1 type
81  * resp: the response to send
82  * returns 1 on success, 0 on failure
83  */
84 int http_server_send_asn1_resp(const char *prog, BIO *cbio, int keep_alive,
85                                const char *content_type,
86                                const ASN1_ITEM *it, const ASN1_VALUE *resp);
87 
88 /*-
89  * Send a trivial HTTP response, typically to report an error or OK
90  * prog: the name of the current app, for diagnostics only
91  * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
92  * status: the status code to send
93  * reason: the corresponding human-readable string
94  * returns 1 on success, 0 on failure
95  */
96 int http_server_send_status(const char *prog, BIO *cbio,
97                             int status, const char *reason);
98 
99 # endif
100 
101 # ifdef HTTP_DAEMON
102 extern int n_responders;
103 extern int acfd;
104 
105 void socket_timeout(int signum);
106 void spawn_loop(const char *prog);
107 # endif
108 
109 #endif
110