httpcli.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/iomgr/sockaddr.h"
  34. #include "src/core/httpcli/httpcli.h"
  35. #include <string.h>
  36. #include "src/core/iomgr/endpoint.h"
  37. #include "src/core/iomgr/resolve_address.h"
  38. #include "src/core/iomgr/tcp_client.h"
  39. #include "src/core/httpcli/format_request.h"
  40. #include "src/core/httpcli/parser.h"
  41. #include "src/core/support/string.h"
  42. #include <grpc/support/alloc.h>
  43. #include <grpc/support/log.h>
  44. #include <grpc/support/string_util.h>
  45. typedef struct {
  46. gpr_slice request_text;
  47. grpc_httpcli_parser parser;
  48. grpc_resolved_addresses *addresses;
  49. size_t next_address;
  50. grpc_endpoint *ep;
  51. char *host;
  52. gpr_timespec deadline;
  53. int have_read_byte;
  54. const grpc_httpcli_handshaker *handshaker;
  55. grpc_httpcli_response_cb on_response;
  56. void *user_data;
  57. grpc_httpcli_context *context;
  58. grpc_pollset *pollset;
  59. grpc_iomgr_object iomgr_obj;
  60. } internal_request;
  61. static grpc_httpcli_get_override g_get_override = NULL;
  62. static grpc_httpcli_post_override g_post_override = NULL;
  63. static void plaintext_handshake(void *arg, grpc_endpoint *endpoint,
  64. const char *host,
  65. void (*on_done)(void *arg,
  66. grpc_endpoint *endpoint)) {
  67. on_done(arg, endpoint);
  68. }
  69. const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http",
  70. plaintext_handshake};
  71. void grpc_httpcli_context_init(grpc_httpcli_context *context) {
  72. grpc_pollset_set_init(&context->pollset_set);
  73. }
  74. void grpc_httpcli_context_destroy(grpc_httpcli_context *context) {
  75. grpc_pollset_set_destroy(&context->pollset_set);
  76. }
  77. static void next_address(internal_request *req);
  78. static void finish(internal_request *req, int success) {
  79. grpc_pollset_set_del_pollset(&req->context->pollset_set, req->pollset);
  80. req->on_response(req->user_data, success ? &req->parser.r : NULL);
  81. grpc_httpcli_parser_destroy(&req->parser);
  82. if (req->addresses != NULL) {
  83. grpc_resolved_addresses_destroy(req->addresses);
  84. }
  85. if (req->ep != NULL) {
  86. grpc_endpoint_destroy(req->ep);
  87. }
  88. gpr_slice_unref(req->request_text);
  89. gpr_free(req->host);
  90. grpc_iomgr_unregister_object(&req->iomgr_obj);
  91. gpr_free(req);
  92. }
  93. static void on_read(void *user_data, gpr_slice *slices, size_t nslices,
  94. grpc_endpoint_cb_status status) {
  95. internal_request *req = user_data;
  96. size_t i;
  97. for (i = 0; i < nslices; i++) {
  98. if (GPR_SLICE_LENGTH(slices[i])) {
  99. req->have_read_byte = 1;
  100. if (!grpc_httpcli_parser_parse(&req->parser, slices[i])) {
  101. finish(req, 0);
  102. goto done;
  103. }
  104. }
  105. }
  106. switch (status) {
  107. case GRPC_ENDPOINT_CB_OK:
  108. grpc_endpoint_notify_on_read(req->ep, on_read, req);
  109. break;
  110. case GRPC_ENDPOINT_CB_EOF:
  111. case GRPC_ENDPOINT_CB_ERROR:
  112. case GRPC_ENDPOINT_CB_SHUTDOWN:
  113. if (!req->have_read_byte) {
  114. next_address(req);
  115. } else {
  116. finish(req, grpc_httpcli_parser_eof(&req->parser));
  117. }
  118. break;
  119. }
  120. done:
  121. for (i = 0; i < nslices; i++) {
  122. gpr_slice_unref(slices[i]);
  123. }
  124. }
  125. static void on_written(internal_request *req) {
  126. grpc_endpoint_notify_on_read(req->ep, on_read, req);
  127. }
  128. static void done_write(void *arg, grpc_endpoint_cb_status status) {
  129. internal_request *req = arg;
  130. switch (status) {
  131. case GRPC_ENDPOINT_CB_OK:
  132. on_written(req);
  133. break;
  134. case GRPC_ENDPOINT_CB_EOF:
  135. case GRPC_ENDPOINT_CB_SHUTDOWN:
  136. case GRPC_ENDPOINT_CB_ERROR:
  137. next_address(req);
  138. break;
  139. }
  140. }
  141. static void start_write(internal_request *req) {
  142. gpr_slice_ref(req->request_text);
  143. switch (
  144. grpc_endpoint_write(req->ep, &req->request_text, 1, done_write, req)) {
  145. case GRPC_ENDPOINT_WRITE_DONE:
  146. on_written(req);
  147. break;
  148. case GRPC_ENDPOINT_WRITE_PENDING:
  149. break;
  150. case GRPC_ENDPOINT_WRITE_ERROR:
  151. finish(req, 0);
  152. break;
  153. }
  154. }
  155. static void on_handshake_done(void *arg, grpc_endpoint *ep) {
  156. internal_request *req = arg;
  157. if (!ep) {
  158. next_address(req);
  159. return;
  160. }
  161. req->ep = ep;
  162. start_write(req);
  163. }
  164. static void on_connected(void *arg, grpc_endpoint *tcp) {
  165. internal_request *req = arg;
  166. if (!tcp) {
  167. next_address(req);
  168. return;
  169. }
  170. req->handshaker->handshake(req, tcp, req->host, on_handshake_done);
  171. }
  172. static void next_address(internal_request *req) {
  173. grpc_resolved_address *addr;
  174. if (req->next_address == req->addresses->naddrs) {
  175. finish(req, 0);
  176. return;
  177. }
  178. addr = &req->addresses->addrs[req->next_address++];
  179. grpc_tcp_client_connect(on_connected, req, &req->context->pollset_set,
  180. (struct sockaddr *)&addr->addr, addr->len,
  181. req->deadline);
  182. }
  183. static void on_resolved(void *arg, grpc_resolved_addresses *addresses) {
  184. internal_request *req = arg;
  185. if (!addresses) {
  186. finish(req, 0);
  187. return;
  188. }
  189. req->addresses = addresses;
  190. req->next_address = 0;
  191. next_address(req);
  192. }
  193. void grpc_httpcli_get(grpc_httpcli_context *context, grpc_pollset *pollset,
  194. const grpc_httpcli_request *request,
  195. gpr_timespec deadline,
  196. grpc_httpcli_response_cb on_response, void *user_data) {
  197. internal_request *req;
  198. char *name;
  199. if (g_get_override &&
  200. g_get_override(request, deadline, on_response, user_data)) {
  201. return;
  202. }
  203. req = gpr_malloc(sizeof(internal_request));
  204. memset(req, 0, sizeof(*req));
  205. req->request_text = grpc_httpcli_format_get_request(request);
  206. grpc_httpcli_parser_init(&req->parser);
  207. req->on_response = on_response;
  208. req->user_data = user_data;
  209. req->deadline = deadline;
  210. req->handshaker =
  211. request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
  212. req->context = context;
  213. req->pollset = pollset;
  214. gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path);
  215. grpc_iomgr_register_object(&req->iomgr_obj, name);
  216. gpr_free(name);
  217. req->host = gpr_strdup(request->host);
  218. grpc_pollset_set_add_pollset(&req->context->pollset_set, req->pollset);
  219. grpc_resolve_address(request->host, req->handshaker->default_port,
  220. on_resolved, req);
  221. }
  222. void grpc_httpcli_post(grpc_httpcli_context *context, grpc_pollset *pollset,
  223. const grpc_httpcli_request *request,
  224. const char *body_bytes, size_t body_size,
  225. gpr_timespec deadline,
  226. grpc_httpcli_response_cb on_response, void *user_data) {
  227. internal_request *req;
  228. char *name;
  229. if (g_post_override && g_post_override(request, body_bytes, body_size,
  230. deadline, on_response, user_data)) {
  231. return;
  232. }
  233. req = gpr_malloc(sizeof(internal_request));
  234. memset(req, 0, sizeof(*req));
  235. req->request_text =
  236. grpc_httpcli_format_post_request(request, body_bytes, body_size);
  237. grpc_httpcli_parser_init(&req->parser);
  238. req->on_response = on_response;
  239. req->user_data = user_data;
  240. req->deadline = deadline;
  241. req->handshaker =
  242. request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
  243. req->context = context;
  244. req->pollset = pollset;
  245. gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path);
  246. grpc_iomgr_register_object(&req->iomgr_obj, name);
  247. gpr_free(name);
  248. req->host = gpr_strdup(request->host);
  249. grpc_pollset_set_add_pollset(&req->context->pollset_set, req->pollset);
  250. grpc_resolve_address(request->host, req->handshaker->default_port,
  251. on_resolved, req);
  252. }
  253. void grpc_httpcli_set_override(grpc_httpcli_get_override get,
  254. grpc_httpcli_post_override post) {
  255. g_get_override = get;
  256. g_post_override = post;
  257. }