• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/http/httpcli.h"
22 
23 #include <string.h>
24 
25 #include <string>
26 
27 #include "absl/strings/str_format.h"
28 
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/string_util.h>
32 
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gprpp/memory.h"
36 #include "src/core/lib/http/format_request.h"
37 #include "src/core/lib/http/parser.h"
38 #include "src/core/lib/iomgr/endpoint.h"
39 #include "src/core/lib/iomgr/iomgr_internal.h"
40 #include "src/core/lib/iomgr/resolve_address.h"
41 #include "src/core/lib/iomgr/sockaddr_utils.h"
42 #include "src/core/lib/iomgr/tcp_client.h"
43 #include "src/core/lib/slice/slice_internal.h"
44 
45 struct internal_request {
46   grpc_slice request_text;
47   grpc_http_parser parser;
48   grpc_resolved_addresses* addresses;
49   size_t next_address;
50   grpc_endpoint* ep;
51   char* host;
52   char* ssl_host_override;
53   grpc_millis deadline;
54   int have_read_byte;
55   const grpc_httpcli_handshaker* handshaker;
56   grpc_closure* on_done;
57   grpc_httpcli_context* context;
58   grpc_polling_entity* pollent;
59   grpc_iomgr_object iomgr_obj;
60   grpc_slice_buffer incoming;
61   grpc_slice_buffer outgoing;
62   grpc_closure on_read;
63   grpc_closure done_write;
64   grpc_closure connected;
65   grpc_error* overall_error;
66   grpc_resource_quota* resource_quota;
67 };
68 static grpc_httpcli_get_override g_get_override = nullptr;
69 static grpc_httpcli_post_override g_post_override = nullptr;
70 
plaintext_handshake(void * arg,grpc_endpoint * endpoint,const char *,grpc_millis,void (* on_done)(void * arg,grpc_endpoint * endpoint))71 static void plaintext_handshake(void* arg, grpc_endpoint* endpoint,
72                                 const char* /*host*/, grpc_millis /*deadline*/,
73                                 void (*on_done)(void* arg,
74                                                 grpc_endpoint* endpoint)) {
75   on_done(arg, endpoint);
76 }
77 
78 const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http",
79                                                         plaintext_handshake};
80 
grpc_httpcli_context_init(grpc_httpcli_context * context)81 void grpc_httpcli_context_init(grpc_httpcli_context* context) {
82   context->pollset_set = grpc_pollset_set_create();
83 }
84 
grpc_httpcli_context_destroy(grpc_httpcli_context * context)85 void grpc_httpcli_context_destroy(grpc_httpcli_context* context) {
86   grpc_pollset_set_destroy(context->pollset_set);
87 }
88 
89 static void next_address(internal_request* req, grpc_error* due_to_error);
90 
finish(internal_request * req,grpc_error * error)91 static void finish(internal_request* req, grpc_error* error) {
92   grpc_polling_entity_del_from_pollset_set(req->pollent,
93                                            req->context->pollset_set);
94   grpc_core::ExecCtx::Run(DEBUG_LOCATION, req->on_done, error);
95   grpc_http_parser_destroy(&req->parser);
96   if (req->addresses != nullptr) {
97     grpc_resolved_addresses_destroy(req->addresses);
98   }
99   if (req->ep != nullptr) {
100     grpc_endpoint_destroy(req->ep);
101   }
102   grpc_slice_unref_internal(req->request_text);
103   gpr_free(req->host);
104   gpr_free(req->ssl_host_override);
105   grpc_iomgr_unregister_object(&req->iomgr_obj);
106   grpc_slice_buffer_destroy_internal(&req->incoming);
107   grpc_slice_buffer_destroy_internal(&req->outgoing);
108   GRPC_ERROR_UNREF(req->overall_error);
109   grpc_resource_quota_unref_internal(req->resource_quota);
110   gpr_free(req);
111 }
112 
append_error(internal_request * req,grpc_error * error)113 static void append_error(internal_request* req, grpc_error* error) {
114   if (req->overall_error == GRPC_ERROR_NONE) {
115     req->overall_error =
116         GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed HTTP/1 client request");
117   }
118   grpc_resolved_address* addr = &req->addresses->addrs[req->next_address - 1];
119   std::string addr_text = grpc_sockaddr_to_uri(addr);
120   req->overall_error = grpc_error_add_child(
121       req->overall_error,
122       grpc_error_set_str(error, GRPC_ERROR_STR_TARGET_ADDRESS,
123                          grpc_slice_from_cpp_string(std::move(addr_text))));
124 }
125 
do_read(internal_request * req)126 static void do_read(internal_request* req) {
127   grpc_endpoint_read(req->ep, &req->incoming, &req->on_read, /*urgent=*/true);
128 }
129 
on_read(void * user_data,grpc_error * error)130 static void on_read(void* user_data, grpc_error* error) {
131   internal_request* req = static_cast<internal_request*>(user_data);
132   size_t i;
133 
134   for (i = 0; i < req->incoming.count; i++) {
135     if (GRPC_SLICE_LENGTH(req->incoming.slices[i])) {
136       req->have_read_byte = 1;
137       grpc_error* err = grpc_http_parser_parse(
138           &req->parser, req->incoming.slices[i], nullptr);
139       if (err != GRPC_ERROR_NONE) {
140         finish(req, err);
141         return;
142       }
143     }
144   }
145 
146   if (error == GRPC_ERROR_NONE) {
147     do_read(req);
148   } else if (!req->have_read_byte) {
149     next_address(req, GRPC_ERROR_REF(error));
150   } else {
151     finish(req, grpc_http_parser_eof(&req->parser));
152   }
153 }
154 
on_written(internal_request * req)155 static void on_written(internal_request* req) { do_read(req); }
156 
done_write(void * arg,grpc_error * error)157 static void done_write(void* arg, grpc_error* error) {
158   internal_request* req = static_cast<internal_request*>(arg);
159   if (error == GRPC_ERROR_NONE) {
160     on_written(req);
161   } else {
162     next_address(req, GRPC_ERROR_REF(error));
163   }
164 }
165 
start_write(internal_request * req)166 static void start_write(internal_request* req) {
167   grpc_slice_ref_internal(req->request_text);
168   grpc_slice_buffer_add(&req->outgoing, req->request_text);
169   grpc_endpoint_write(req->ep, &req->outgoing, &req->done_write, nullptr);
170 }
171 
on_handshake_done(void * arg,grpc_endpoint * ep)172 static void on_handshake_done(void* arg, grpc_endpoint* ep) {
173   internal_request* req = static_cast<internal_request*>(arg);
174 
175   if (!ep) {
176     next_address(req, GRPC_ERROR_CREATE_FROM_STATIC_STRING(
177                           "Unexplained handshake failure"));
178     return;
179   }
180 
181   req->ep = ep;
182   start_write(req);
183 }
184 
on_connected(void * arg,grpc_error * error)185 static void on_connected(void* arg, grpc_error* error) {
186   internal_request* req = static_cast<internal_request*>(arg);
187 
188   if (!req->ep) {
189     next_address(req, GRPC_ERROR_REF(error));
190     return;
191   }
192   req->handshaker->handshake(
193       req, req->ep, req->ssl_host_override ? req->ssl_host_override : req->host,
194       req->deadline, on_handshake_done);
195 }
196 
next_address(internal_request * req,grpc_error * error)197 static void next_address(internal_request* req, grpc_error* error) {
198   grpc_resolved_address* addr;
199   if (error != GRPC_ERROR_NONE) {
200     append_error(req, error);
201   }
202   if (req->next_address == req->addresses->naddrs) {
203     finish(req,
204            GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
205                "Failed HTTP requests to all targets", &req->overall_error, 1));
206     return;
207   }
208   addr = &req->addresses->addrs[req->next_address++];
209   GRPC_CLOSURE_INIT(&req->connected, on_connected, req,
210                     grpc_schedule_on_exec_ctx);
211   grpc_arg arg = grpc_channel_arg_pointer_create(
212       const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA), req->resource_quota,
213       grpc_resource_quota_arg_vtable());
214   grpc_channel_args args = {1, &arg};
215   grpc_tcp_client_connect(&req->connected, &req->ep, req->context->pollset_set,
216                           &args, addr, req->deadline);
217 }
218 
on_resolved(void * arg,grpc_error * error)219 static void on_resolved(void* arg, grpc_error* error) {
220   internal_request* req = static_cast<internal_request*>(arg);
221   if (error != GRPC_ERROR_NONE) {
222     finish(req, GRPC_ERROR_REF(error));
223     return;
224   }
225   req->next_address = 0;
226   next_address(req, GRPC_ERROR_NONE);
227 }
228 
internal_request_begin(grpc_httpcli_context * context,grpc_polling_entity * pollent,grpc_resource_quota * resource_quota,const grpc_httpcli_request * request,grpc_millis deadline,grpc_closure * on_done,grpc_httpcli_response * response,const char * name,const grpc_slice & request_text)229 static void internal_request_begin(grpc_httpcli_context* context,
230                                    grpc_polling_entity* pollent,
231                                    grpc_resource_quota* resource_quota,
232                                    const grpc_httpcli_request* request,
233                                    grpc_millis deadline, grpc_closure* on_done,
234                                    grpc_httpcli_response* response,
235                                    const char* name,
236                                    const grpc_slice& request_text) {
237   internal_request* req =
238       static_cast<internal_request*>(gpr_malloc(sizeof(internal_request)));
239   memset(req, 0, sizeof(*req));
240   req->request_text = request_text;
241   grpc_http_parser_init(&req->parser, GRPC_HTTP_RESPONSE, response);
242   req->on_done = on_done;
243   req->deadline = deadline;
244   req->handshaker =
245       request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
246   req->context = context;
247   req->pollent = pollent;
248   req->overall_error = GRPC_ERROR_NONE;
249   req->resource_quota = grpc_resource_quota_ref_internal(resource_quota);
250   GRPC_CLOSURE_INIT(&req->on_read, on_read, req, grpc_schedule_on_exec_ctx);
251   GRPC_CLOSURE_INIT(&req->done_write, done_write, req,
252                     grpc_schedule_on_exec_ctx);
253   grpc_slice_buffer_init(&req->incoming);
254   grpc_slice_buffer_init(&req->outgoing);
255   grpc_iomgr_register_object(&req->iomgr_obj, name);
256   req->host = gpr_strdup(request->host);
257   req->ssl_host_override = gpr_strdup(request->ssl_host_override);
258 
259   GPR_ASSERT(pollent);
260   grpc_polling_entity_add_to_pollset_set(req->pollent,
261                                          req->context->pollset_set);
262   grpc_resolve_address(
263       request->host, req->handshaker->default_port, req->context->pollset_set,
264       GRPC_CLOSURE_CREATE(on_resolved, req, grpc_schedule_on_exec_ctx),
265       &req->addresses);
266 }
267 
grpc_httpcli_get(grpc_httpcli_context * context,grpc_polling_entity * pollent,grpc_resource_quota * resource_quota,const grpc_httpcli_request * request,grpc_millis deadline,grpc_closure * on_done,grpc_httpcli_response * response)268 void grpc_httpcli_get(grpc_httpcli_context* context,
269                       grpc_polling_entity* pollent,
270                       grpc_resource_quota* resource_quota,
271                       const grpc_httpcli_request* request, grpc_millis deadline,
272                       grpc_closure* on_done, grpc_httpcli_response* response) {
273   if (g_get_override && g_get_override(request, deadline, on_done, response)) {
274     return;
275   }
276   std::string name =
277       absl::StrFormat("HTTP:GET:%s:%s", request->host, request->http.path);
278   internal_request_begin(context, pollent, resource_quota, request, deadline,
279                          on_done, response, name.c_str(),
280                          grpc_httpcli_format_get_request(request));
281 }
282 
grpc_httpcli_post(grpc_httpcli_context * context,grpc_polling_entity * pollent,grpc_resource_quota * resource_quota,const grpc_httpcli_request * request,const char * body_bytes,size_t body_size,grpc_millis deadline,grpc_closure * on_done,grpc_httpcli_response * response)283 void grpc_httpcli_post(grpc_httpcli_context* context,
284                        grpc_polling_entity* pollent,
285                        grpc_resource_quota* resource_quota,
286                        const grpc_httpcli_request* request,
287                        const char* body_bytes, size_t body_size,
288                        grpc_millis deadline, grpc_closure* on_done,
289                        grpc_httpcli_response* response) {
290   if (g_post_override && g_post_override(request, body_bytes, body_size,
291                                          deadline, on_done, response)) {
292     return;
293   }
294   std::string name =
295       absl::StrFormat("HTTP:POST:%s:%s", request->host, request->http.path);
296   internal_request_begin(
297       context, pollent, resource_quota, request, deadline, on_done, response,
298       name.c_str(),
299       grpc_httpcli_format_post_request(request, body_bytes, body_size));
300 }
301 
grpc_httpcli_set_override(grpc_httpcli_get_override get,grpc_httpcli_post_override post)302 void grpc_httpcli_set_override(grpc_httpcli_get_override get,
303                                grpc_httpcli_post_override post) {
304   g_get_override = get;
305   g_post_override = post;
306 }
307