Prepare for 3.0 alpha 3
[oweals/openssl.git] / apps / include / http_server.h
1 /*
2  * Copyright 1995-2020 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_INFO
39 #  undef LOG_WARNING
40 #  undef LOG_ERR
41 #  define LOG_INFO      0
42 #  define LOG_WARNING   1
43 #  define LOG_ERR       2
44 # endif
45
46 /*-
47  * Log a message to syslog if multi-threaded HTTP_DAEMON, else to bio_err
48  * prog: the name of the current app
49  * level: the severity of the message, e.g., LOG_ERR
50  * fmt: message with potential extra parameters like with printf()
51  * returns nothing
52  */
53 void log_message(const char *prog, int level, const char *fmt, ...);
54
55 # ifndef OPENSSL_NO_SOCK
56 /*-
57  * Initialize an HTTP server by setting up its listening BIO
58  * prog: the name of the current app
59  * port: the port to listen on
60  * returns a BIO for accepting requests, NULL on error
61  */
62 BIO *http_server_init_bio(const char *prog, const char *port);
63 /*-
64  * Accept an ASN.1-formatted HTTP request
65  * it: the expected request ASN.1 type
66  * preq: pointer to variable where to place the parsed request
67  * pcbio: pointer to variable where to place the BIO for sending the response to
68  * acbio: the listening bio (typically as returned by http_server_init_bio())
69  * prog: the name of the current app
70  * accept_get: wheter to accept GET requests (in addition to POST requests)
71  * timeout: connection timeout (in seconds), or 0 for none/infinite
72  * returns 0 in case caller should retry, then *preq == *pcbio == NULL
73  * returns -1 on fatal error; also in this case *preq == *pcbio == NULL
74  * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL
75  *                      while *preq == NULL if and only if request is invalid
76  */
77 int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
78                         BIO **pcbio, BIO *acbio,
79                         const char *prog, int accept_get, int timeout);
80 /*-
81  * Send an ASN.1-formatted HTTP response
82  * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
83  *       note: cbio should not do an encoding that changes the output length
84  * content_type: string identifying the type of the response
85  * it: the response ASN.1 type
86  * valit: the response ASN.1 type
87  * resp: the response to send
88  * returns 1 on success, 0 on failure
89  */
90 int http_server_send_asn1_resp(BIO *cbio, const char *content_type,
91                                const ASN1_ITEM *it, const ASN1_VALUE *resp);
92 # endif
93
94 # ifdef HTTP_DAEMON
95 extern int multi;
96 extern int acfd;
97
98 void socket_timeout(int signum);
99 void spawn_loop(const char *prog);
100 # endif
101
102 #endif