httpread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * httpread - Manage reading file(s) from HTTP/TCP socket
  3. * Author: Ted Merrill
  4. * Copyright 2008 Atheros Communications
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #ifndef HTTPREAD_H
  10. #define HTTPREAD_H
  11. /* event types (passed to callback) */
  12. enum httpread_event {
  13. HTTPREAD_EVENT_FILE_READY = 1, /* including reply ready */
  14. HTTPREAD_EVENT_TIMEOUT = 2,
  15. HTTPREAD_EVENT_ERROR = 3 /* misc. error, esp malloc error */
  16. };
  17. /* header type detected
  18. * available to callback via call to httpread_reply_code_get()
  19. */
  20. enum httpread_hdr_type {
  21. HTTPREAD_HDR_TYPE_UNKNOWN = 0, /* none of the following */
  22. HTTPREAD_HDR_TYPE_REPLY = 1, /* hdr begins w/ HTTP/ */
  23. HTTPREAD_HDR_TYPE_GET = 2, /* hdr begins with GET<sp> */
  24. HTTPREAD_HDR_TYPE_HEAD = 3, /* hdr begins with HEAD<sp> */
  25. HTTPREAD_HDR_TYPE_POST = 4, /* hdr begins with POST<sp> */
  26. HTTPREAD_HDR_TYPE_PUT = 5, /* hdr begins with ... */
  27. HTTPREAD_HDR_TYPE_DELETE = 6, /* hdr begins with ... */
  28. HTTPREAD_HDR_TYPE_TRACE = 7, /* hdr begins with ... */
  29. HTTPREAD_HDR_TYPE_CONNECT = 8, /* hdr begins with ... */
  30. HTTPREAD_HDR_TYPE_NOTIFY = 9, /* hdr begins with ... */
  31. HTTPREAD_HDR_TYPE_M_SEARCH = 10, /* hdr begins with ... */
  32. HTTPREAD_HDR_TYPE_M_POST = 11, /* hdr begins with ... */
  33. HTTPREAD_HDR_TYPE_SUBSCRIBE = 12, /* hdr begins with ... */
  34. HTTPREAD_HDR_TYPE_UNSUBSCRIBE = 13, /* hdr begins with ... */
  35. HTTPREAD_N_HDR_TYPES /* keep last */
  36. };
  37. /* control instance -- opaque struct declaration
  38. */
  39. struct httpread;
  40. /* httpread_destroy -- if h is non-NULL, clean up
  41. * This must eventually be called by the application following
  42. * call of the application's callback and may be called
  43. * earlier if desired.
  44. */
  45. void httpread_destroy(struct httpread *h);
  46. /* httpread_create -- start a new reading session making use of eloop.
  47. * The new instance will use the socket descriptor for reading (until
  48. * it gets a file and not after) but will not close the socket, even
  49. * when the instance is destroyed (the application must do that).
  50. * Return NULL on error.
  51. *
  52. * Provided that httpread_create successfully returns a handle,
  53. * the callback fnc is called to handle httpread_event events.
  54. * The caller should do destroy on any errors or unknown events.
  55. *
  56. * Pass max_bytes == 0 to not read body at all (required for e.g.
  57. * reply to HEAD request).
  58. */
  59. struct httpread * httpread_create(
  60. int sd, /* descriptor of TCP socket to read from */
  61. void (*cb)(struct httpread *handle, void *cookie,
  62. enum httpread_event e), /* call on event */
  63. void *cookie, /* pass to callback */
  64. int max_bytes, /* maximum file size else abort it */
  65. int timeout_seconds /* 0; or total duration timeout period */
  66. );
  67. /* httpread_hdr_type_get -- When file is ready, returns header type.
  68. */
  69. enum httpread_hdr_type httpread_hdr_type_get(struct httpread *h);
  70. /* httpread_uri_get -- When file is ready, uri_get returns (translated) URI
  71. * or possibly NULL (which would be an error).
  72. */
  73. char *httpread_uri_get(struct httpread *h);
  74. /* httpread_reply_code_get -- When reply is ready, returns reply code */
  75. int httpread_reply_code_get(struct httpread *h);
  76. /* httpread_length_get -- When file is ready, returns file length. */
  77. int httpread_length_get(struct httpread *h);
  78. /* httpread_data_get -- When file is ready, returns file content
  79. * with null byte appened.
  80. * Might return NULL in some error condition.
  81. */
  82. void * httpread_data_get(struct httpread *h);
  83. /* httpread_hdr_get -- When file is ready, returns header content
  84. * with null byte appended.
  85. * Might return NULL in some error condition.
  86. */
  87. char * httpread_hdr_get(struct httpread *h);
  88. /* httpread_hdr_line_get -- When file is ready, returns pointer
  89. * to line within header content matching the given tag
  90. * (after the tag itself and any spaces/tabs).
  91. *
  92. * The tag should end with a colon for reliable matching.
  93. *
  94. * If not found, returns NULL;
  95. */
  96. char * httpread_hdr_line_get(struct httpread *h, const char *tag);
  97. #endif /* HTTPREAD_H */