shttpd.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2004-2008 Sergey Lyubka <valenok@gmail.com>
  3. * All rights reserved
  4. *
  5. * "THE BEER-WARE LICENSE" (Revision 42):
  6. * Sergey Lyubka wrote this file. As long as you retain this notice you
  7. * can do whatever you want with this stuff. If we meet some day, and you think
  8. * this stuff is worth it, you can buy me a beer in return.
  9. *
  10. * $Id: shttpd.h,v 1.18 2008/08/23 08:34:50 drozd Exp $
  11. */
  12. #ifndef SHTTPD_HEADER_INCLUDED
  13. #define SHTTPD_HEADER_INCLUDED
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif /* __cplusplus */
  17. struct ubuf {
  18. char *buf; /* Buffer pointer */
  19. int len; /* Size of a buffer */
  20. int num_bytes; /* Bytes processed by callback */
  21. };
  22. /*
  23. * This structure is passed to the user callback function
  24. */
  25. struct shttpd_arg {
  26. void *priv; /* Private! Do not touch! */
  27. void *state; /* User state */
  28. void *user_data; /* Data from register_uri() */
  29. struct ubuf in; /* Input is here, POST data */
  30. struct ubuf out; /* Output goes here */
  31. unsigned int flags;
  32. #define SHTTPD_END_OF_OUTPUT 1 /* No more data do send */
  33. #define SHTTPD_CONNECTION_ERROR 2 /* Server closed the connection */
  34. #define SHTTPD_MORE_POST_DATA 4 /* arg->in has incomplete data */
  35. #define SHTTPD_POST_BUFFER_FULL 8 /* arg->in has max data */
  36. #define SHTTPD_SSI_EVAL_TRUE 16 /* SSI eval callback must set it*/
  37. #define SHTTPD_SUSPEND 32 /* User wants to suspend output */
  38. };
  39. /*
  40. * User callback function. Called when certain registered URLs have been
  41. * requested. These are the requirements to the callback function:
  42. *
  43. * 1. It must copy data into 'out.buf' buffer, not more than 'out.len' bytes,
  44. * and record how many bytes are copied, into 'out.num_bytes'
  45. * 2. It must not call any blocking functions
  46. * 3. It must set SHTTPD_END_OF_OUTPUT flag when there is no more data to send
  47. * 4. For POST requests, it must process the incoming data (in.buf) of length
  48. * 'in.len', and set 'in.num_bytes', which is how many bytes of POST
  49. * data was processed and can be discarded by SHTTPD.
  50. * 5. If callback allocates arg->state, to keep state, it must deallocate it
  51. * at the end of coonection SHTTPD_CONNECTION_ERROR or SHTTPD_END_OF_OUTPUT
  52. * 6. If callback function wants to suspend until some event, it must store
  53. * arg->priv pointer elsewhere, set SHTTPD_SUSPEND flag and return. When
  54. * the event happens, user code should call shttpd_wakeup(priv).
  55. * It is safe to call shttpd_wakeup() from any thread. User code must
  56. * not call shttpd_wakeup once the connection is closed.
  57. */
  58. typedef void (*shttpd_callback_t)(struct shttpd_arg *);
  59. /*
  60. * shttpd_init Initialize shttpd context
  61. * shttpd_fini Dealocate the context, close all connections
  62. * shttpd_set_option Set new value for option
  63. * shttpd_register_uri Setup the callback function for specified URL
  64. * shttpd_poll Do connections processing
  65. * shttpd_version return string with SHTTPD version
  66. * shttpd_get_var Fetch POST/GET variable value by name. Return value len
  67. * shttpd_get_header return value of the specified HTTP header
  68. * shttpd_get_env return values for the following pseudo-variables:
  69. "REQUEST_METHOD", "REQUEST_URI",
  70. * "REMOTE_USER" and "REMOTE_ADDR"
  71. * shttpd_printf helper function to output data
  72. * shttpd_handle_error register custom HTTP error handler
  73. * shttpd_wakeup clear SHTTPD_SUSPEND state for the connection
  74. */
  75. struct shttpd_ctx;
  76. struct shttpd_ctx *shttpd_init(int argc, char *argv[]);
  77. int shttpd_set_option(struct shttpd_ctx *, const char *opt, const char *val);
  78. void shttpd_fini(struct shttpd_ctx *);
  79. void shttpd_register_uri(struct shttpd_ctx *ctx, const char *uri,
  80. shttpd_callback_t callback, void *const user_data);
  81. void shttpd_poll(struct shttpd_ctx *, int milliseconds);
  82. const char *shttpd_version(void);
  83. int shttpd_get_var(const char *var, const char *buf, int buf_len,
  84. char *value, int value_len);
  85. const char *shttpd_get_header(struct shttpd_arg *, const char *header_name);
  86. const char *shttpd_get_boundary(struct shttpd_arg *, int *boundary_length);
  87. const char *shttpd_get_env(struct shttpd_arg *, const char *name);
  88. void shttpd_get_http_version(struct shttpd_arg *,
  89. unsigned long *major, unsigned long *minor);
  90. size_t shttpd_printf(struct shttpd_arg *, const char *fmt, ...);
  91. void shttpd_handle_error(struct shttpd_ctx *ctx, int status,
  92. shttpd_callback_t func, void *const data);
  93. void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name,
  94. shttpd_callback_t func, void *const user_data);
  95. void shttpd_wakeup(const void *priv);
  96. int shttpd_join(struct shttpd_ctx *, fd_set *, fd_set *, int *max_fd);
  97. int shttpd_socketpair(int sp[2]);
  98. #ifdef __cplusplus
  99. }
  100. #endif /* __cplusplus */
  101. #endif /* SHTTPD_HEADER_INCLUDED */