http_server.h 3.9 KB

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