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