Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 34 | #include "src/core/http/httpcli.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 35 | #include "src/core/iomgr/sockaddr.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <string.h> |
| 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
Craig Tiller | 1d7704d | 2016-02-25 14:07:29 -0800 | [diff] [blame] | 42 | |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 43 | #include "src/core/http/format_request.h" |
| 44 | #include "src/core/http/parser.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 45 | #include "src/core/iomgr/endpoint.h" |
| 46 | #include "src/core/iomgr/iomgr_internal.h" |
| 47 | #include "src/core/iomgr/resolve_address.h" |
| 48 | #include "src/core/iomgr/tcp_client.h" |
| 49 | #include "src/core/support/string.h" |
| 50 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 51 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | gpr_slice request_text; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 53 | grpc_http_parser parser; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | grpc_resolved_addresses *addresses; |
| 55 | size_t next_address; |
| 56 | grpc_endpoint *ep; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | char *host; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 58 | char *ssl_host_override; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | gpr_timespec deadline; |
| 60 | int have_read_byte; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 61 | const grpc_httpcli_handshaker *handshaker; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | grpc_httpcli_response_cb on_response; |
| 63 | void *user_data; |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 64 | grpc_httpcli_context *context; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 65 | grpc_pollset *pollset; |
Craig Tiller | f39f2d6 | 2015-06-06 14:43:06 -0700 | [diff] [blame] | 66 | grpc_iomgr_object iomgr_obj; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 67 | gpr_slice_buffer incoming; |
| 68 | gpr_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; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | } internal_request; |
| 73 | |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 74 | static grpc_httpcli_get_override g_get_override = NULL; |
| 75 | static grpc_httpcli_post_override g_post_override = NULL; |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | static void plaintext_handshake(grpc_exec_ctx *exec_ctx, void *arg, |
| 78 | grpc_endpoint *endpoint, const char *host, |
| 79 | void (*on_done)(grpc_exec_ctx *exec_ctx, |
| 80 | void *arg, |
| 81 | grpc_endpoint *endpoint)) { |
| 82 | on_done(exec_ctx, arg, endpoint); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http", |
| 86 | plaintext_handshake}; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | void grpc_httpcli_context_init(grpc_httpcli_context *context) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 89 | context->pollset_set = grpc_pollset_set_create(); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | void grpc_httpcli_context_destroy(grpc_httpcli_context *context) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 93 | grpc_pollset_set_destroy(context->pollset_set); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | static void finish(grpc_exec_ctx *exec_ctx, internal_request *req, |
| 99 | int success) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 100 | grpc_pollset_set_del_pollset(exec_ctx, req->context->pollset_set, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | req->pollset); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 102 | req->on_response(exec_ctx, req->user_data, |
| 103 | success ? &req->parser.http.response : NULL); |
| 104 | grpc_http_parser_destroy(&req->parser); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | if (req->addresses != NULL) { |
| 106 | grpc_resolved_addresses_destroy(req->addresses); |
| 107 | } |
| 108 | if (req->ep != NULL) { |
| 109 | grpc_endpoint_destroy(exec_ctx, req->ep); |
| 110 | } |
| 111 | gpr_slice_unref(req->request_text); |
| 112 | gpr_free(req->host); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 113 | gpr_free(req->ssl_host_override); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | grpc_iomgr_unregister_object(&req->iomgr_obj); |
| 115 | gpr_slice_buffer_destroy(&req->incoming); |
| 116 | gpr_slice_buffer_destroy(&req->outgoing); |
| 117 | gpr_free(req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 120 | static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, bool success); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 121 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | static void do_read(grpc_exec_ctx *exec_ctx, internal_request *req) { |
| 123 | grpc_endpoint_read(exec_ctx, req->ep, &req->incoming, &req->on_read); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 126 | static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, bool success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | internal_request *req = user_data; |
| 128 | size_t i; |
| 129 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | for (i = 0; i < req->incoming.count; i++) { |
| 131 | if (GPR_SLICE_LENGTH(req->incoming.slices[i])) { |
| 132 | req->have_read_byte = 1; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 133 | if (!grpc_http_parser_parse(&req->parser, req->incoming.slices[i])) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | finish(exec_ctx, req, 0); |
| 135 | return; |
| 136 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | if (success) { |
| 141 | do_read(exec_ctx, req); |
| 142 | } else if (!req->have_read_byte) { |
| 143 | next_address(exec_ctx, req); |
| 144 | } else { |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 145 | int parse_success = grpc_http_parser_eof(&req->parser); |
| 146 | if (parse_success && (req->parser.type != GRPC_HTTP_RESPONSE)) { |
| 147 | parse_success = 0; |
| 148 | } |
| 149 | finish(exec_ctx, req, parse_success); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | static void on_written(grpc_exec_ctx *exec_ctx, internal_request *req) { |
| 154 | do_read(exec_ctx, req); |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 155 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 157 | static void done_write(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | internal_request *req = arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | if (success) { |
| 160 | on_written(exec_ctx, req); |
| 161 | } else { |
| 162 | next_address(exec_ctx, req); |
| 163 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | static void start_write(grpc_exec_ctx *exec_ctx, internal_request *req) { |
| 167 | gpr_slice_ref(req->request_text); |
| 168 | gpr_slice_buffer_add(&req->outgoing, req->request_text); |
| 169 | grpc_endpoint_write(exec_ctx, req->ep, &req->outgoing, &req->done_write); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg, |
| 173 | grpc_endpoint *ep) { |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 174 | internal_request *req = arg; |
| 175 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | if (!ep) { |
| 177 | next_address(exec_ctx, req); |
| 178 | return; |
| 179 | } |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 180 | |
| 181 | req->ep = ep; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | start_write(exec_ctx, req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 185 | static void on_connected(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | internal_request *req = arg; |
| 187 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | if (!req->ep) { |
| 189 | next_address(exec_ctx, req); |
| 190 | return; |
| 191 | } |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 192 | req->handshaker->handshake( |
| 193 | exec_ctx, req, req->ep, |
| 194 | req->ssl_host_override ? req->ssl_host_override : req->host, |
| 195 | on_handshake_done); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | grpc_resolved_address *addr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | if (req->next_address == req->addresses->naddrs) { |
| 201 | finish(exec_ctx, req, 0); |
| 202 | return; |
| 203 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | addr = &req->addresses->addrs[req->next_address++]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | grpc_closure_init(&req->connected, on_connected, req); |
| 206 | grpc_tcp_client_connect( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 207 | exec_ctx, &req->connected, &req->ep, req->context->pollset_set, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | (struct sockaddr *)&addr->addr, addr->len, req->deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | static void on_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
| 212 | grpc_resolved_addresses *addresses) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | internal_request *req = arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | if (!addresses) { |
| 215 | finish(exec_ctx, req, 0); |
| 216 | return; |
| 217 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 218 | req->addresses = addresses; |
| 219 | req->next_address = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | next_address(exec_ctx, req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | static void internal_request_begin( |
| 224 | grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context, |
| 225 | grpc_pollset *pollset, const grpc_httpcli_request *request, |
| 226 | gpr_timespec deadline, grpc_httpcli_response_cb on_response, |
| 227 | void *user_data, const char *name, gpr_slice request_text) { |
| 228 | internal_request *req = gpr_malloc(sizeof(internal_request)); |
| 229 | memset(req, 0, sizeof(*req)); |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 230 | req->request_text = request_text; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 231 | grpc_http_parser_init(&req->parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | req->on_response = on_response; |
| 233 | req->user_data = user_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 234 | req->deadline = deadline; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | req->handshaker = |
| 236 | request->handshaker ? request->handshaker : &grpc_httpcli_plaintext; |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 237 | req->context = context; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 238 | req->pollset = pollset; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | grpc_closure_init(&req->on_read, on_read, req); |
| 240 | grpc_closure_init(&req->done_write, done_write, req); |
| 241 | gpr_slice_buffer_init(&req->incoming); |
| 242 | gpr_slice_buffer_init(&req->outgoing); |
| 243 | grpc_iomgr_register_object(&req->iomgr_obj, name); |
| 244 | req->host = gpr_strdup(request->host); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 245 | req->ssl_host_override = gpr_strdup(request->ssl_host_override); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 247 | grpc_pollset_set_add_pollset(exec_ctx, req->context->pollset_set, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 248 | req->pollset); |
| 249 | grpc_resolve_address(request->host, req->handshaker->default_port, |
| 250 | on_resolved, req); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context, |
| 254 | grpc_pollset *pollset, |
| 255 | const grpc_httpcli_request *request, |
| 256 | gpr_timespec deadline, |
| 257 | grpc_httpcli_response_cb on_response, void *user_data) { |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 258 | char *name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 259 | if (g_get_override && |
| 260 | g_get_override(exec_ctx, request, deadline, on_response, user_data)) { |
| 261 | return; |
| 262 | } |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 263 | gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->http.path); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | internal_request_begin(exec_ctx, context, pollset, request, deadline, |
| 265 | on_response, user_data, name, |
| 266 | grpc_httpcli_format_get_request(request)); |
| 267 | gpr_free(name); |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 270 | void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context, |
| 271 | grpc_pollset *pollset, |
| 272 | const grpc_httpcli_request *request, |
| 273 | const char *body_bytes, size_t body_size, |
| 274 | gpr_timespec deadline, |
| 275 | grpc_httpcli_response_cb on_response, void *user_data) { |
Craig Tiller | f39f2d6 | 2015-06-06 14:43:06 -0700 | [diff] [blame] | 276 | char *name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | if (g_post_override && |
| 278 | g_post_override(exec_ctx, request, body_bytes, body_size, deadline, |
| 279 | on_response, user_data)) { |
| 280 | return; |
| 281 | } |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 282 | gpr_asprintf(&name, "HTTP:POST:%s:%s", request->host, request->http.path); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | internal_request_begin( |
| 284 | exec_ctx, context, pollset, request, deadline, on_response, user_data, |
| 285 | name, grpc_httpcli_format_post_request(request, body_bytes, body_size)); |
| 286 | gpr_free(name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | } |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 288 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | void grpc_httpcli_set_override(grpc_httpcli_get_override get, |
| 290 | grpc_httpcli_post_override post) { |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 291 | g_get_override = get; |
| 292 | g_post_override = post; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 293 | } |