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