http_server.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 1995-2021 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_DEBUG
  34. # undef LOG_INFO
  35. # undef LOG_WARNING
  36. # undef LOG_ERR
  37. # define LOG_DEBUG 7
  38. # define LOG_INFO 6
  39. # define LOG_WARNING 4
  40. # define LOG_ERR 3
  41. # endif
  42. /*-
  43. * Log a message to syslog if multi-threaded HTTP_DAEMON, else to bio_err
  44. * prog: the name of the current app
  45. * level: the severity of the message, e.g., LOG_ERR
  46. * fmt: message with potential extra parameters like with printf()
  47. * returns nothing
  48. */
  49. void log_message(const char *prog, int level, const char *fmt, ...);
  50. # ifndef OPENSSL_NO_SOCK
  51. /*-
  52. * Initialize an HTTP server by setting up its listening BIO
  53. * prog: the name of the current app
  54. * port: the port to listen on
  55. * returns a BIO for accepting requests, NULL on error
  56. */
  57. BIO *http_server_init_bio(const char *prog, const char *port);
  58. /*-
  59. * Accept an ASN.1-formatted HTTP request
  60. * it: the expected request ASN.1 type
  61. * preq: pointer to variable where to place the parsed request
  62. * ppath: pointer to variable where to place the request path, or NULL
  63. * pcbio: pointer to variable where to place the BIO for sending the response to
  64. * acbio: the listening bio (typically as returned by http_server_init_bio())
  65. * found_keep_alive: for returning flag if client requests persistent connection
  66. * prog: the name of the current app, for diagnostics only
  67. * port: the local port listening to, for diagnostics only
  68. * accept_get: whether to accept GET requests (in addition to POST requests)
  69. * timeout: connection timeout (in seconds), or 0 for none/infinite
  70. * returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL
  71. * returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL
  72. * returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while
  73. * *ppath == NULL and *preq == NULL if and only if the request is invalid,
  74. * On return value 1 the caller is responsible for sending an HTTP response,
  75. * using http_server_send_asn1_resp() or http_server_send_status().
  76. * The caller must free any non-NULL *preq, *ppath, and *pcbio pointers.
  77. */
  78. int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
  79. char **ppath, BIO **pcbio, BIO *acbio,
  80. int *found_keep_alive,
  81. const char *prog, const char *port,
  82. int accept_get, int timeout);
  83. /*-
  84. * Send an ASN.1-formatted HTTP response
  85. * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
  86. * note: cbio should not do an encoding that changes the output length
  87. * keep_alive: grant persistent connnection
  88. * content_type: string identifying the type of the response
  89. * it: the response ASN.1 type
  90. * resp: the response to send
  91. * returns 1 on success, 0 on failure
  92. */
  93. int http_server_send_asn1_resp(BIO *cbio, int keep_alive,
  94. const char *content_type,
  95. const ASN1_ITEM *it, const ASN1_VALUE *resp);
  96. /*-
  97. * Send a trivial HTTP response, typically to report an error or OK
  98. * cbio: destination BIO (typically as returned by http_server_get_asn1_req())
  99. * status: the status code to send
  100. * reason: the corresponding human-readable string
  101. * returns 1 on success, 0 on failure
  102. */
  103. int http_server_send_status(BIO *cbio, int status, const char *reason);
  104. # endif
  105. # ifdef HTTP_DAEMON
  106. extern int multi;
  107. extern int acfd;
  108. void socket_timeout(int signum);
  109. void spawn_loop(const char *prog);
  110. # endif
  111. #endif