2578922-evhttp_set_chunk_cb.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. commit 62429424b8f71bef8b37f565bae64de4ceb93378
  2. author: aozeritsky
  3. date: 2016-12-02T13:26:22+03:00
  4. revision: 2578922
  5. [LOGBROKER-2064] evhttp_set_chunk_cb patch from here
  6. http://archives.seul.org/libevent/users/Apr-2011/msg00038.html
  7. REVIEW: 216641
  8. commit 04044a6dfe9a9665a46526024d2056af848d3ea8
  9. author: aozeritsky
  10. date: 2016-12-09T15:13:12+03:00
  11. revision: 2587034
  12. [LOGBROKER-2064] follow up for http chunked
  13. REVIEW: 218703
  14. --- libevent/http-internal.h (a2df0aadb743a91ec4876eed1b9678939de28570)
  15. +++ libevent/http-internal.h (index)
  16. @@ -112,6 +112,7 @@ struct evhttp_cb {
  17. TAILQ_ENTRY(evhttp_cb) next;
  18. char *what;
  19. + int chunked;
  20. void (*cb)(struct evhttp_request *req, void *);
  21. void *cbarg;
  22. --- libevent/http.c (a2df0aadb743a91ec4876eed1b9678939de28570)
  23. +++ libevent/http.c (index)
  24. @@ -200,6 +200,9 @@ static void evhttp_write_buffer(struct evhttp_connection *,
  25. void (*)(struct evhttp_connection *, void *), void *);
  26. static void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
  27. +static struct evhttp_cb *
  28. +evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req, int chunked);
  29. +
  30. /* callbacks for bufferevent */
  31. static void evhttp_read_cb(struct bufferevent *, void *);
  32. static void evhttp_write_cb(struct bufferevent *, void *);
  33. @@ -971,7 +974,7 @@ evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf)
  34. req->ntoread = -1;
  35. if (req->chunk_cb != NULL) {
  36. req->flags |= EVHTTP_REQ_DEFER_FREE;
  37. - (*req->chunk_cb)(req, req->cb_arg);
  38. + (*req->chunk_cb)(req, req->chunk_cb_arg);
  39. evbuffer_drain(req->input_buffer,
  40. evbuffer_get_length(req->input_buffer));
  41. req->flags &= ~EVHTTP_REQ_DEFER_FREE;
  42. @@ -981,6 +984,12 @@ evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf)
  43. }
  44. }
  45. + if (req->chunk_cb != NULL) {
  46. + req->flags |= EVHTTP_REQ_PROCESS_CHUNKS_END;
  47. + (*req->chunk_cb)(req, req->chunk_cb_arg);
  48. + req->flags &= ~EVHTTP_REQ_PROCESS_CHUNKS_END;
  49. + }
  50. +
  51. return (MORE_DATA_EXPECTED);
  52. }
  53. @@ -1094,7 +1103,7 @@ evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
  54. if (evbuffer_get_length(req->input_buffer) > 0 && req->chunk_cb != NULL) {
  55. req->flags |= EVHTTP_REQ_DEFER_FREE;
  56. - (*req->chunk_cb)(req, req->cb_arg);
  57. + (*req->chunk_cb)(req, req->chunk_cb_arg);
  58. req->flags &= ~EVHTTP_REQ_DEFER_FREE;
  59. evbuffer_drain(req->input_buffer,
  60. evbuffer_get_length(req->input_buffer));
  61. @@ -1900,6 +1909,14 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line, size_t len)
  62. !evhttp_find_vhost(req->evcon->http_server, NULL, hostname))
  63. req->flags |= EVHTTP_PROXY_REQUEST;
  64. + {
  65. + struct evhttp_cb * chunkcb = evhttp_dispatch_callback(&req->evcon->http_server->callbacks, req, 1);
  66. + if (chunkcb) {
  67. + req->chunk_cb = chunkcb->cb;
  68. + req->chunk_cb_arg = chunkcb->cbarg;
  69. + }
  70. + }
  71. +
  72. return 0;
  73. }
  74. @@ -3361,7 +3378,7 @@ evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers)
  75. }
  76. static struct evhttp_cb *
  77. -evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
  78. +evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req, int chunked)
  79. {
  80. struct evhttp_cb *cb;
  81. size_t offset = 0;
  82. @@ -3378,8 +3395,13 @@ evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
  83. TAILQ_FOREACH(cb, callbacks, next) {
  84. if (!strcmp(cb->what, translated)) {
  85. - mm_free(translated);
  86. - return (cb);
  87. + if (chunked < 0) {
  88. + mm_free(translated);
  89. + return (cb);
  90. + } else if (chunked == cb->chunked) {
  91. + mm_free(translated);
  92. + return (cb);
  93. + }
  94. }
  95. }
  96. @@ -3520,7 +3542,7 @@ evhttp_handle_request(struct evhttp_request *req, void *arg)
  97. evhttp_find_vhost(http, &http, hostname);
  98. }
  99. - if ((cb = evhttp_dispatch_callback(&http->callbacks, req)) != NULL) {
  100. + if ((cb = evhttp_dispatch_callback(&http->callbacks, req, -1)) != NULL) {
  101. (*cb->cb)(req, cb->cbarg);
  102. return;
  103. }
  104. @@ -3940,8 +3962,8 @@ evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods)
  105. }
  106. int
  107. -evhttp_set_cb(struct evhttp *http, const char *uri,
  108. - void (*cb)(struct evhttp_request *, void *), void *cbarg)
  109. +evhttp_set_cb_internal(struct evhttp *http, const char *uri,
  110. + void (*cb)(struct evhttp_request *, void *), void *cbarg, int chunked)
  111. {
  112. struct evhttp_cb *http_cb;
  113. @@ -3963,6 +3985,7 @@ evhttp_set_cb(struct evhttp *http, const char *uri,
  114. }
  115. http_cb->cb = cb;
  116. http_cb->cbarg = cbarg;
  117. + http_cb->chunked = chunked;
  118. TAILQ_INSERT_TAIL(&http->callbacks, http_cb, next);
  119. @@ -3970,6 +3993,19 @@ evhttp_set_cb(struct evhttp *http, const char *uri,
  120. }
  121. int
  122. +evhttp_set_cb(struct evhttp *http, const char *uri,
  123. + void (*cb)(struct evhttp_request *, void *), void *cbarg)
  124. +{
  125. + return evhttp_set_cb_internal(http, uri, cb, cbarg, 0);
  126. +}
  127. +
  128. +int evhttp_set_chunk_cb(struct evhttp *http, const char *path,
  129. + void (*chunk_cb)(struct evhttp_request *, void *), void *cb_arg)
  130. +{
  131. + return evhttp_set_cb_internal(http, path, chunk_cb, cb_arg, 1);
  132. +}
  133. +
  134. +int
  135. evhttp_del_cb(struct evhttp *http, const char *uri)
  136. {
  137. struct evhttp_cb *http_cb;
  138. @@ -4118,9 +4154,10 @@ evhttp_connection_get_base(struct evhttp_connection *conn)
  139. void
  140. evhttp_request_set_chunked_cb(struct evhttp_request *req,
  141. - void (*cb)(struct evhttp_request *, void *))
  142. + void (*cb)(struct evhttp_request *, void *), void *arg)
  143. {
  144. req->chunk_cb = cb;
  145. + req->chunk_cb_arg = arg;
  146. }
  147. void
  148. --- libevent/include/event2/http.h (a2df0aadb743a91ec4876eed1b9678939de28570)
  149. +++ libevent/include/event2/http.h (index)
  150. @@ -264,6 +264,10 @@ EVENT2_EXPORT_SYMBOL
  151. int evhttp_set_cb(struct evhttp *http, const char *path,
  152. void (*cb)(struct evhttp_request *, void *), void *cb_arg);
  153. +EVENT2_EXPORT_SYMBOL
  154. +int evhttp_set_chunk_cb(struct evhttp *http, const char *path,
  155. + void (*chunk_cb)(struct evhttp_request *, void *), void *cb_arg);
  156. +
  157. /** Removes the callback for a specified URI */
  158. EVENT2_EXPORT_SYMBOL
  159. int evhttp_del_cb(struct evhttp *, const char *);
  160. @@ -594,7 +598,7 @@ struct evhttp_request *evhttp_request_new(
  161. */
  162. EVENT2_EXPORT_SYMBOL
  163. void evhttp_request_set_chunked_cb(struct evhttp_request *,
  164. - void (*cb)(struct evhttp_request *, void *));
  165. + void (*cb)(struct evhttp_request *, void *), void *arg);
  166. /**
  167. * Register callback for additional parsing of request headers.
  168. --- libevent/include/event2/http_struct.h (a2df0aadb743a91ec4876eed1b9678939de28570)
  169. +++ libevent/include/event2/http_struct.h (index)
  170. @@ -78,6 +78,8 @@ struct {
  171. /** The request should be freed upstack */
  172. #define EVHTTP_REQ_NEEDS_FREE 0x0010
  173. +#define EVHTTP_REQ_PROCESS_CHUNKS_END 0x0024
  174. +
  175. struct evkeyvalq *input_headers;
  176. struct evkeyvalq *output_headers;
  177. @@ -120,6 +122,7 @@ struct {
  178. * the regular callback.
  179. */
  180. void (*chunk_cb)(struct evhttp_request *, void *);
  181. + void *chunk_cb_arg;
  182. /*
  183. * Callback added for forked-daapd so they can collect ICY