Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/http/httpcli.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 40 | #include <grpc/support/string_util.h> |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 41 | #include <grpc/support/useful.h> |
Craig Tiller | 1d7704d | 2016-02-25 14:07:29 -0800 | [diff] [blame] | 42 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/http/format_request.h" |
| 44 | #include "src/core/lib/http/parser.h" |
| 45 | #include "src/core/lib/iomgr/endpoint.h" |
| 46 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 47 | #include "src/core/lib/iomgr/resolve_address.h" |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 49 | #include "src/core/lib/iomgr/tcp_client.h" |
| 50 | #include "src/core/lib/support/string.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | typedef struct { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 53 | grpc_slice request_text; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 54 | grpc_http_parser parser; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | grpc_resolved_addresses *addresses; |
| 56 | size_t next_address; |
| 57 | grpc_endpoint *ep; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | char *host; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 59 | char *ssl_host_override; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | gpr_timespec deadline; |
| 61 | int have_read_byte; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 62 | const grpc_httpcli_handshaker *handshaker; |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 63 | grpc_closure *on_done; |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 64 | grpc_httpcli_context *context; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 65 | grpc_polling_entity *pollent; |
Craig Tiller | f39f2d6 | 2015-06-06 14:43:06 -0700 | [diff] [blame] | 66 | grpc_iomgr_object iomgr_obj; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 67 | grpc_slice_buffer incoming; |
| 68 | grpc_slice_buffer outgoing; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 69 | grpc_closure on_read; |
| 70 | grpc_closure done_write; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 71 | grpc_closure connected; |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 72 | grpc_error *overall_error; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 73 | grpc_resource_quota *resource_quota; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | } internal_request; |
| 75 | |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 76 | static grpc_httpcli_get_override g_get_override = NULL; |
| 77 | static grpc_httpcli_post_override g_post_override = NULL; |
| 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | static void plaintext_handshake(grpc_exec_ctx *exec_ctx, void *arg, |
| 80 | grpc_endpoint *endpoint, const char *host, |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 81 | gpr_timespec deadline, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | void (*on_done)(grpc_exec_ctx *exec_ctx, |
| 83 | void *arg, |
| 84 | grpc_endpoint *endpoint)) { |
| 85 | on_done(exec_ctx, arg, endpoint); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http", |
| 89 | plaintext_handshake}; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 90 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | void grpc_httpcli_context_init(grpc_httpcli_context *context) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 92 | context->pollset_set = grpc_pollset_set_create(); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | void grpc_httpcli_context_destroy(grpc_httpcli_context *context) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 96 | grpc_pollset_set_destroy(context->pollset_set); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 99 | static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req, |
| 100 | grpc_error *due_to_error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static void finish(grpc_exec_ctx *exec_ctx, internal_request *req, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 103 | grpc_error *error) { |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 104 | grpc_polling_entity_del_from_pollset_set(exec_ctx, req->pollent, |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 105 | req->context->pollset_set); |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 106 | grpc_exec_ctx_sched(exec_ctx, req->on_done, error, NULL); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 107 | grpc_http_parser_destroy(&req->parser); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | if (req->addresses != NULL) { |
| 109 | grpc_resolved_addresses_destroy(req->addresses); |
| 110 | } |
| 111 | if (req->ep != NULL) { |
| 112 | grpc_endpoint_destroy(exec_ctx, req->ep); |
| 113 | } |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 114 | grpc_slice_unref(req->request_text); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | gpr_free(req->host); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 116 | gpr_free(req->ssl_host_override); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | grpc_iomgr_unregister_object(&req->iomgr_obj); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 118 | grpc_slice_buffer_destroy(&req->incoming); |
| 119 | grpc_slice_buffer_destroy(&req->outgoing); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 120 | GRPC_ERROR_UNREF(req->overall_error); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 121 | grpc_resource_quota_internal_unref(exec_ctx, req->resource_quota); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | gpr_free(req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 125 | static void append_error(internal_request *req, grpc_error *error) { |
| 126 | if (req->overall_error == GRPC_ERROR_NONE) { |
| 127 | req->overall_error = GRPC_ERROR_CREATE("Failed HTTP/1 client request"); |
| 128 | } |
| 129 | grpc_resolved_address *addr = &req->addresses->addrs[req->next_address - 1]; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 130 | char *addr_text = grpc_sockaddr_to_uri(addr); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 131 | req->overall_error = grpc_error_add_child( |
| 132 | req->overall_error, |
| 133 | grpc_error_set_str(error, GRPC_ERROR_STR_TARGET_ADDRESS, addr_text)); |
| 134 | gpr_free(addr_text); |
| 135 | } |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | static void do_read(grpc_exec_ctx *exec_ctx, internal_request *req) { |
| 138 | grpc_endpoint_read(exec_ctx, req->ep, &req->incoming, &req->on_read); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 141 | static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, |
| 142 | grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | internal_request *req = user_data; |
| 144 | size_t i; |
| 145 | |
Craig Tiller | e6deeb1 | 2016-05-09 13:14:32 -0700 | [diff] [blame] | 146 | for (i = 0; i < req->incoming.count; i++) { |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame^] | 147 | if (GRPC_SLICE_LENGTH(req->incoming.slices[i])) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | req->have_read_byte = 1; |
Craig Tiller | e6deeb1 | 2016-05-09 13:14:32 -0700 | [diff] [blame] | 149 | grpc_error *err = |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame] | 150 | grpc_http_parser_parse(&req->parser, req->incoming.slices[i], NULL); |
Craig Tiller | e6deeb1 | 2016-05-09 13:14:32 -0700 | [diff] [blame] | 151 | if (err != GRPC_ERROR_NONE) { |
| 152 | finish(exec_ctx, req, err); |
| 153 | return; |
| 154 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 158 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | do_read(exec_ctx, req); |
| 160 | } else if (!req->have_read_byte) { |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 161 | next_address(exec_ctx, req, GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | } else { |
Craig Tiller | e6deeb1 | 2016-05-09 13:14:32 -0700 | [diff] [blame] | 163 | finish(exec_ctx, req, grpc_http_parser_eof(&req->parser)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 164 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | static void on_written(grpc_exec_ctx *exec_ctx, internal_request *req) { |
| 168 | do_read(exec_ctx, req); |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 169 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 171 | static void done_write(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | internal_request *req = arg; |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 173 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | on_written(exec_ctx, req); |
| 175 | } else { |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 176 | next_address(exec_ctx, req, GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | static void start_write(grpc_exec_ctx *exec_ctx, internal_request *req) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 181 | grpc_slice_ref(req->request_text); |
| 182 | grpc_slice_buffer_add(&req->outgoing, req->request_text); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | grpc_endpoint_write(exec_ctx, req->ep, &req->outgoing, &req->done_write); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg, |
| 187 | grpc_endpoint *ep) { |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 188 | internal_request *req = arg; |
| 189 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | if (!ep) { |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 191 | next_address(exec_ctx, req, |
| 192 | GRPC_ERROR_CREATE("Unexplained handshake failure")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 195 | |
| 196 | req->ep = ep; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 197 | start_write(exec_ctx, req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 200 | static void on_connected(grpc_exec_ctx *exec_ctx, void *arg, |
| 201 | grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | internal_request *req = arg; |
| 203 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | if (!req->ep) { |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 205 | next_address(exec_ctx, req, GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | return; |
| 207 | } |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 208 | req->handshaker->handshake( |
| 209 | exec_ctx, req, req->ep, |
| 210 | req->ssl_host_override ? req->ssl_host_override : req->host, |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 211 | req->deadline, on_handshake_done); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 214 | static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req, |
| 215 | grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | grpc_resolved_address *addr; |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 217 | if (error != GRPC_ERROR_NONE) { |
| 218 | append_error(req, error); |
| 219 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | if (req->next_address == req->addresses->naddrs) { |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 221 | finish(exec_ctx, req, |
| 222 | GRPC_ERROR_CREATE_REFERENCING("Failed HTTP requests to all targets", |
| 223 | &req->overall_error, 1)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 226 | addr = &req->addresses->addrs[req->next_address++]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | grpc_closure_init(&req->connected, on_connected, req); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 228 | grpc_arg arg; |
Craig Tiller | 153eaa7 | 2016-10-21 13:52:36 -0700 | [diff] [blame] | 229 | arg.key = GRPC_ARG_RESOURCE_QUOTA; |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 230 | arg.type = GRPC_ARG_POINTER; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 231 | arg.value.pointer.p = req->resource_quota; |
| 232 | arg.value.pointer.vtable = grpc_resource_quota_arg_vtable(); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 233 | grpc_channel_args args = {1, &arg}; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 234 | grpc_tcp_client_connect(exec_ctx, &req->connected, &req->ep, |
Craig Tiller | 3d357d9 | 2016-10-26 20:52:03 -0700 | [diff] [blame] | 235 | req->context->pollset_set, &args, addr, |
| 236 | req->deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 239 | static void on_resolved(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 240 | internal_request *req = arg; |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 241 | if (error != GRPC_ERROR_NONE) { |
| 242 | finish(exec_ctx, req, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 245 | req->next_address = 0; |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 246 | next_address(exec_ctx, req, GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 249 | static void internal_request_begin(grpc_exec_ctx *exec_ctx, |
| 250 | grpc_httpcli_context *context, |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 251 | grpc_polling_entity *pollent, |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 252 | grpc_resource_quota *resource_quota, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 253 | const grpc_httpcli_request *request, |
| 254 | gpr_timespec deadline, grpc_closure *on_done, |
| 255 | grpc_httpcli_response *response, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 256 | const char *name, grpc_slice request_text) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | internal_request *req = gpr_malloc(sizeof(internal_request)); |
| 258 | memset(req, 0, sizeof(*req)); |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 259 | req->request_text = request_text; |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 260 | grpc_http_parser_init(&req->parser, GRPC_HTTP_RESPONSE, response); |
| 261 | req->on_done = on_done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 262 | req->deadline = deadline; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | req->handshaker = |
| 264 | request->handshaker ? request->handshaker : &grpc_httpcli_plaintext; |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 265 | req->context = context; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 266 | req->pollent = pollent; |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 267 | req->overall_error = GRPC_ERROR_NONE; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 268 | req->resource_quota = grpc_resource_quota_internal_ref(resource_quota); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | grpc_closure_init(&req->on_read, on_read, req); |
| 270 | grpc_closure_init(&req->done_write, done_write, req); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 271 | grpc_slice_buffer_init(&req->incoming); |
| 272 | grpc_slice_buffer_init(&req->outgoing); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 273 | grpc_iomgr_register_object(&req->iomgr_obj, name); |
| 274 | req->host = gpr_strdup(request->host); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 275 | req->ssl_host_override = gpr_strdup(request->ssl_host_override); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 276 | |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 277 | GPR_ASSERT(pollent); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 278 | grpc_polling_entity_add_to_pollset_set(exec_ctx, req->pollent, |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 279 | req->context->pollset_set); |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 280 | grpc_resolve_address(exec_ctx, request->host, req->handshaker->default_port, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 281 | grpc_closure_create(on_resolved, req), &req->addresses); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 284 | void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 285 | grpc_polling_entity *pollent, |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 286 | grpc_resource_quota *resource_quota, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | const grpc_httpcli_request *request, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 288 | gpr_timespec deadline, grpc_closure *on_done, |
| 289 | grpc_httpcli_response *response) { |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 290 | char *name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 291 | if (g_get_override && |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 292 | g_get_override(exec_ctx, request, deadline, on_done, response)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 295 | gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->http.path); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 296 | internal_request_begin(exec_ctx, context, pollent, resource_quota, request, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 297 | deadline, on_done, response, name, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | grpc_httpcli_format_get_request(request)); |
| 299 | gpr_free(name); |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 302 | void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 303 | grpc_polling_entity *pollent, |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 304 | grpc_resource_quota *resource_quota, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 305 | const grpc_httpcli_request *request, |
| 306 | const char *body_bytes, size_t body_size, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 307 | gpr_timespec deadline, grpc_closure *on_done, |
| 308 | grpc_httpcli_response *response) { |
Craig Tiller | f39f2d6 | 2015-06-06 14:43:06 -0700 | [diff] [blame] | 309 | char *name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 310 | if (g_post_override && |
| 311 | g_post_override(exec_ctx, request, body_bytes, body_size, deadline, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 312 | on_done, response)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 315 | gpr_asprintf(&name, "HTTP:POST:%s:%s", request->host, request->http.path); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 316 | internal_request_begin( |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 317 | exec_ctx, context, pollent, resource_quota, request, deadline, on_done, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 318 | response, name, |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 319 | grpc_httpcli_format_post_request(request, body_bytes, body_size)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | gpr_free(name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 321 | } |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 322 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | void grpc_httpcli_set_override(grpc_httpcli_get_override get, |
| 324 | grpc_httpcli_post_override post) { |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 325 | g_get_override = get; |
| 326 | g_post_override = post; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 327 | } |