2
0

http_server.h 3.9 KB

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