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