xref: /freebsd/crypto/openssl/apps/include/http_server.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*
2  * Copyright 1995-2021 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 
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 */
37 # else
38 #  undef LOG_DEBUG
39 #  undef LOG_INFO
40 #  undef LOG_WARNING
41 #  undef LOG_ERR
42 #  define LOG_DEBUG     7
43 #  define LOG_INFO      6
44 #  define LOG_WARNING   4
45 #  define LOG_ERR       3
46 # endif
47 
48 /*-
49  * Log a message to syslog if multi-threaded HTTP_DAEMON, else to bio_err
50  * prog: the name of the current app
51  * level: the severity of the message, e.g., LOG_ERR
52  * fmt: message with potential extra parameters like with printf()
53  * returns nothing
54  */
55 void log_message(const char *prog, int level, const char *fmt, ...);
56 
57 # ifndef OPENSSL_NO_SOCK
58 /*-
59  * Initialize an HTTP server by setting up its listening BIO
60  * prog: the name of the current app
61  * port: the port to listen on
62  * returns a BIO for accepting requests, NULL on error
63  */
64 BIO *http_server_init_bio(const char *prog, const char *port);
65 
66 /*-
67  * Accept an ASN.1-formatted HTTP request
68  * it: the expected request ASN.1 type
69  * preq: pointer to variable where to place the parsed request
70  * ppath: pointer to variable where to place the request path, or NULL
71  * pcbio: pointer to variable where to place the BIO for sending the response to
72  * acbio: the listening bio (typically as returned by http_server_init_bio())
73  * found_keep_alive: for returning flag if client requests persistent connection
74  * prog: the name of the current app, for diagnostics only
75  * port: the local port listening to, for diagnostics only
76  * accept_get: whether to accept GET requests (in addition to POST requests)
77  * timeout: connection timeout (in seconds), or 0 for none/infinite
78  * returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL
79  * returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL
80  * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while
81  * *ppath == NULL and *preq == NULL if and only if the request is invalid,
82  * On return value 1 the caller is responsible for sending an HTTP response,
83  * using http_server_send_asn1_resp() or http_server_send_status().
84  * The caller must free any non-NULL *preq, *ppath, and *pcbio pointers.
85  */
86 int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
87                              char **ppath, BIO **pcbio, BIO *acbio,
88                              int *found_keep_alive,
89                              const char *prog, const char *port,
90                              int accept_get, int timeout);
91 
92 /*-
93  * Send an ASN.1-formatted HTTP response
94  * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
95  *       note: cbio should not do an encoding that changes the output length
96  * keep_alive: grant persistent connnection
97  * content_type: string identifying the type of the response
98  * it: the response ASN.1 type
99  * resp: the response to send
100  * returns 1 on success, 0 on failure
101  */
102 int http_server_send_asn1_resp(BIO *cbio, int keep_alive,
103                                const char *content_type,
104                                const ASN1_ITEM *it, const ASN1_VALUE *resp);
105 
106 /*-
107  * Send a trivial HTTP response, typically to report an error or OK
108  * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
109  * status: the status code to send
110  * reason: the corresponding human-readable string
111  * returns 1 on success, 0 on failure
112  */
113 int http_server_send_status(BIO *cbio, int status, const char *reason);
114 
115 # endif
116 
117 # ifdef HTTP_DAEMON
118 extern int multi;
119 extern int acfd;
120 
121 void socket_timeout(int signum);
122 void spawn_loop(const char *prog);
123 # endif
124 
125 #endif
126