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