2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_HTTP_SERVER_H
11 # define OSSL_HTTP_SERVER_H
16 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
29 # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
30 && !defined(OPENSSL_NO_POSIX_IO)
32 # include <sys/types.h>
33 # include <sys/wait.h>
36 # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
47 # define LOG_WARNING 4
51 * Output a message using the trace API with the given category
52 * if the category is >= 0 and tracing is enabled.
53 * Log the message to syslog if multi-threaded HTTP_DAEMON, else to bio_err
54 * if the verbosity is sufficient for the given level of severity.
55 * category: trace category as defined in trace.h, or -1
56 * prog: the name of the current app, or NULL
57 * level: the severity of the message, e.g., LOG_ERR
58 * fmt: message format, which should not include a trailing newline
59 * ...: potential extra parameters like with printf()
62 void trace_log_message(int category,
63 const char *prog, int level, const char *fmt, ...);
65 # ifndef OPENSSL_NO_SOCK
67 * Initialize an HTTP server, setting up its listening BIO
68 * prog: the name of the current app
69 * port: the port to listen on
70 * verbosity: the level of verbosity to use, or -1 for default: LOG_INFO
71 * returns a BIO for accepting requests, NULL on error
73 BIO *http_server_init(const char *prog, const char *port, int verbosity);
76 * Accept an ASN.1-formatted HTTP request
77 * it: the expected request ASN.1 type
78 * preq: pointer to variable where to place the parsed request
79 * ppath: pointer to variable where to place the request path, or NULL
80 * pcbio: pointer to variable where to place the BIO for sending the response to
81 * acbio: the listening bio (typically as returned by http_server_init_bio())
82 * found_keep_alive: for returning flag if client requests persistent connection
83 * prog: the name of the current app, for diagnostics only
84 * accept_get: whether to accept GET requests (in addition to POST requests)
85 * timeout: connection timeout (in seconds), or 0 for none/infinite
86 * returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL
87 * returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL
88 * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while
89 * *ppath == NULL and *preq == NULL if and only if the request is invalid,
90 * On return value 1 the caller is responsible for sending an HTTP response,
91 * using http_server_send_asn1_resp() or http_server_send_status().
92 * The caller must free any non-NULL *preq, *ppath, and *pcbio pointers.
94 int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
95 char **ppath, BIO **pcbio, BIO *acbio,
96 int *found_keep_alive,
97 const char *prog, int accept_get, int timeout);
100 * Send an ASN.1-formatted HTTP response
101 * prog: the name of the current app, for diagnostics only
102 * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
103 * note: cbio should not do an encoding that changes the output length
104 * keep_alive: grant persistent connection
105 * content_type: string identifying the type of the response
106 * it: the response ASN.1 type
107 * resp: the response to send
108 * returns 1 on success, 0 on failure
110 int http_server_send_asn1_resp(const char *prog, BIO *cbio, int keep_alive,
111 const char *content_type,
112 const ASN1_ITEM *it, const ASN1_VALUE *resp);
115 * Send a trivial HTTP response, typically to report an error or OK
116 * prog: the name of the current app, for diagnostics only
117 * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
118 * status: the status code to send
119 * reason: the corresponding human-readable string
120 * returns 1 on success, 0 on failure
122 int http_server_send_status(const char *prog, BIO *cbio,
123 int status, const char *reason);
131 void socket_timeout(int signum);
132 void spawn_loop(const char *prog);