• 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 #ifndef GRPC_SRC_CORE_UTIL_HTTP_CLIENT_HTTPCLI_H
20 #define GRPC_SRC_CORE_UTIL_HTTP_CLIENT_HTTPCLI_H
21 
22 #include <grpc/event_engine/event_engine.h>
23 #include <grpc/grpc.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/port_platform.h>
26 #include <stddef.h>
27 
28 #include <functional>
29 #include <memory>
30 #include <vector>
31 
32 #include "absl/base/thread_annotations.h"
33 #include "absl/status/status.h"
34 #include "absl/status/statusor.h"
35 #include "absl/strings/string_view.h"
36 #include "absl/types/optional.h"
37 #include "src/core/handshaker/handshaker.h"
38 #include "src/core/lib/iomgr/closure.h"
39 #include "src/core/lib/iomgr/endpoint.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/exec_ctx.h"
42 #include "src/core/lib/iomgr/iomgr_fwd.h"
43 #include "src/core/lib/iomgr/iomgr_internal.h"
44 #include "src/core/lib/iomgr/polling_entity.h"
45 #include "src/core/lib/iomgr/resolve_address.h"
46 #include "src/core/lib/resource_quota/resource_quota.h"
47 #include "src/core/util/debug_location.h"
48 #include "src/core/util/http_client/parser.h"
49 #include "src/core/util/orphanable.h"
50 #include "src/core/util/ref_counted_ptr.h"
51 #include "src/core/util/sync.h"
52 #include "src/core/util/time.h"
53 #include "src/core/util/uri.h"
54 
55 // User agent this library reports
56 #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
57 
58 // override functions return 1 if they handled the request, 0 otherwise
59 typedef int (*grpc_httpcli_get_override)(const grpc_http_request* request,
60                                          const grpc_core::URI& uri,
61                                          grpc_core::Timestamp deadline,
62                                          grpc_closure* on_complete,
63                                          grpc_http_response* response);
64 typedef int (*grpc_httpcli_post_override)(const grpc_http_request* request,
65                                           const grpc_core::URI& uri,
66                                           absl::string_view body,
67                                           grpc_core::Timestamp deadline,
68                                           grpc_closure* on_complete,
69                                           grpc_http_response* response);
70 typedef int (*grpc_httpcli_put_override)(const grpc_http_request* request,
71                                          const grpc_core::URI& uri,
72                                          absl::string_view body,
73                                          grpc_core::Timestamp deadline,
74                                          grpc_closure* on_complete,
75                                          grpc_http_response* response);
76 
77 namespace grpc_core {
78 
79 // Tracks an in-progress GET or POST request. Calling \a Start()
80 // begins async work and calling \a Orphan() arranges for async work
81 // to be completed as sooon as possible (possibly aborting the request
82 // if it's in flight).
83 // TODO(ctiller): allow caching and capturing multiple requests for the
84 //                same content and combining them
85 class HttpRequest : public InternallyRefCounted<HttpRequest> {
86  public:
87   // Asynchronously perform a HTTP GET.
88   // 'uri' is the target to make the request to. The scheme field is used to
89   //  determine the port number. The authority field is the target host. The
90   //  path field determines the path of the request. No other fields are used.
91   // 'args' are optional channel args for the request.
92   // 'pollent' indicates a grpc_polling_entity that is interested in the result
93   //   of the get - work on this entity may be used to progress the get
94   //   operation
95   // 'request' contains request parameters - these are caller owned and
96   //   can be destroyed once the call returns
97   // 'deadline' contains a deadline for the request (or gpr_inf_future)
98   // 'on_done' is a callback to report results to
99   // 'channel_creds' are used to configurably secure the connection.
100   //   For insecure requests, use grpc_insecure_credentials_create.
101   //   For secure requests, use CreateHttpRequestSSLCredentials().
102   //   nullptr is treated as insecure credentials.
103   //   TODO(yihuaz): disallow nullptr as a value after unsecure builds
104   //   are removed.
105   GRPC_MUST_USE_RESULT static OrphanablePtr<HttpRequest> Get(
106       URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
107       const grpc_http_request* request, Timestamp deadline,
108       grpc_closure* on_done, grpc_http_response* response,
109       RefCountedPtr<grpc_channel_credentials> channel_creds);
110 
111   // Asynchronously perform a HTTP POST.
112   // 'uri' is the target to make the request to. The scheme field is used to
113   //  determine the port number. The authority field is the target host. The
114   //  path field determines the path of the request. No other fields are used.
115   // 'args' are optional channel args for the request.
116   // 'pollent' indicates a grpc_polling_entity that is interested in the result
117   //   of the post - work on this entity may be used to progress the post
118   //   operation
119   // 'request' contains request parameters - these are caller owned and can be
120   //   destroyed once the call returns
121   // 'deadline' contains a deadline for the request (or gpr_inf_future)
122   // 'on_done' is a callback to report results to
123   // 'channel_creds' are used to configurably secure the connection.
124   //   For insecure requests, use grpc_insecure_credentials_create.
125   //   For secure requests, use CreateHttpRequestSSLCredentials().
126   //   nullptr is treated as insecure credentials.
127   //   TODO(apolcyn): disallow nullptr as a value after unsecure builds
128   //   are removed.
129   // Does not support ?var1=val1&var2=val2 in the path.
130   GRPC_MUST_USE_RESULT static OrphanablePtr<HttpRequest> Post(
131       URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
132       const grpc_http_request* request, Timestamp deadline,
133       grpc_closure* on_done, grpc_http_response* response,
134       RefCountedPtr<grpc_channel_credentials> channel_creds);
135 
136   // Asynchronously perform a HTTP PUT.
137   // 'uri' is the target to make the request to. The scheme field is used to
138   //  determine the port number. The authority field is the target host. The
139   //  path field determines the path of the request. No other fields are used.
140   // 'args' are optional channel args for the request.
141   // 'pollent' indicates a grpc_polling_entity that is interested in the result
142   //   of the post - work on this entity may be used to progress the post
143   //   operation
144   // 'request' contains request parameters - these are caller owned and can be
145   //   destroyed once the call returns
146   // 'deadline' contains a deadline for the request (or gpr_inf_future)
147   // 'on_done' is a callback to report results to
148   // 'channel_creds' are used to configurably secure the connection.
149   //   For insecure requests, use grpc_insecure_credentials_create.
150   //   For secure requests, use CreateHttpRequestSSLCredentials().
151   //   nullptr is treated as insecure credentials.
152   //   TODO(apolcyn): disallow nullptr as a value after unsecure builds
153   //   are removed.
154   // Does not support ?var1=val1&var2=val2 in the path.
155   GRPC_MUST_USE_RESULT static OrphanablePtr<HttpRequest> Put(
156       URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
157       const grpc_http_request* request, Timestamp deadline,
158       grpc_closure* on_done, grpc_http_response* response,
159       RefCountedPtr<grpc_channel_credentials> channel_creds);
160 
161   HttpRequest(URI uri, const grpc_slice& request_text,
162               grpc_http_response* response, Timestamp deadline,
163               const grpc_channel_args* channel_args, grpc_closure* on_done,
164               grpc_polling_entity* pollent, const char* name,
165               absl::optional<std::function<bool()>> test_only_generate_response,
166               RefCountedPtr<grpc_channel_credentials> channel_creds);
167 
168   ~HttpRequest() override;
169 
170   void Start();
171 
172   void Orphan() override;
173 
174   static void SetOverride(grpc_httpcli_get_override get,
175                           grpc_httpcli_post_override post,
176                           grpc_httpcli_put_override put);
177 
178   static void TestOnlySetOnHandshakeDoneIntercept(
179       void (*intercept)(HttpRequest* req));
180 
181  private:
Finish(grpc_error_handle error)182   void Finish(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
183     grpc_polling_entity_del_from_pollset_set(pollent_, pollset_set_);
184     ExecCtx::Run(DEBUG_LOCATION, on_done_, error);
185   }
186 
187   void AppendError(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
188 
DoRead()189   void DoRead() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
190     Ref().release();  // ref held by pending read
191     grpc_endpoint_read(ep_.get(), &incoming_, &on_read_, /*urgent=*/true,
192                        /*min_progress_size=*/1);
193   }
194 
OnRead(void * user_data,grpc_error_handle error)195   static void OnRead(void* user_data, grpc_error_handle error) {
196     HttpRequest* req = static_cast<HttpRequest*>(user_data);
197     ExecCtx::Run(DEBUG_LOCATION,
198                  &req->continue_on_read_after_schedule_on_exec_ctx_, error);
199   }
200 
201   // Needed since OnRead may be called inline from grpc_endpoint_read
ContinueOnReadAfterScheduleOnExecCtx(void * user_data,grpc_error_handle error)202   static void ContinueOnReadAfterScheduleOnExecCtx(void* user_data,
203                                                    grpc_error_handle error) {
204     RefCountedPtr<HttpRequest> req(static_cast<HttpRequest*>(user_data));
205     MutexLock lock(&req->mu_);
206     req->OnReadInternal(error);
207   }
208 
209   void OnReadInternal(grpc_error_handle error)
210       ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
211 
OnWritten()212   void OnWritten() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) { DoRead(); }
213 
DoneWrite(void * arg,grpc_error_handle error)214   static void DoneWrite(void* arg, grpc_error_handle error) {
215     HttpRequest* req = static_cast<HttpRequest*>(arg);
216     ExecCtx::Run(DEBUG_LOCATION,
217                  &req->continue_done_write_after_schedule_on_exec_ctx_, error);
218   }
219 
220   // Needed since DoneWrite may be called inline from grpc_endpoint_write
221   static void ContinueDoneWriteAfterScheduleOnExecCtx(void* arg,
222                                                       grpc_error_handle error);
223 
224   void StartWrite() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
225 
226   void OnHandshakeDone(absl::StatusOr<HandshakerArgs*> result);
227 
228   void DoHandshake(
229       const grpc_event_engine::experimental::EventEngine::ResolvedAddress& addr)
230       ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
231 
232   void NextAddress(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
233 
234   void OnResolved(
235       absl::StatusOr<std::vector<
236           grpc_event_engine::experimental::EventEngine::ResolvedAddress>>
237           addresses_or);
238 
239   const URI uri_;
240   const grpc_slice request_text_;
241   const Timestamp deadline_;
242   const grpc_channel_args* channel_args_;
243   RefCountedPtr<grpc_channel_credentials> channel_creds_;
244   grpc_closure on_read_;
245   grpc_closure continue_on_read_after_schedule_on_exec_ctx_;
246   grpc_closure done_write_;
247   grpc_closure continue_done_write_after_schedule_on_exec_ctx_;
248   OrphanablePtr<grpc_endpoint> ep_;
249   grpc_closure* on_done_;
250   ResourceQuotaRefPtr resource_quota_;
251   grpc_polling_entity* pollent_;
252   grpc_pollset_set* pollset_set_;
253   const absl::optional<std::function<bool()>> test_only_generate_response_;
254   Mutex mu_;
255   RefCountedPtr<HandshakeManager> handshake_mgr_ ABSL_GUARDED_BY(mu_);
256   bool cancelled_ ABSL_GUARDED_BY(mu_) = false;
257   grpc_http_parser parser_ ABSL_GUARDED_BY(mu_);
258   std::vector<grpc_event_engine::experimental::EventEngine::ResolvedAddress>
259       addresses_ ABSL_GUARDED_BY(mu_);
260   size_t next_address_ ABSL_GUARDED_BY(mu_) = 0;
261   int have_read_byte_ ABSL_GUARDED_BY(mu_) = 0;
262   grpc_iomgr_object iomgr_obj_ ABSL_GUARDED_BY(mu_);
263   grpc_slice_buffer incoming_ ABSL_GUARDED_BY(mu_);
264   grpc_slice_buffer outgoing_ ABSL_GUARDED_BY(mu_);
265   grpc_error_handle overall_error_ ABSL_GUARDED_BY(mu_) = absl::OkStatus();
266   // TODO(yijiem): remove these once event_engine_dns_non_client_channel
267   // experiment is fully enabled.
268   bool use_event_engine_dns_resolver_;
269   std::shared_ptr<DNSResolver> resolver_;
270   absl::optional<DNSResolver::TaskHandle> dns_request_handle_
271       ABSL_GUARDED_BY(mu_) = DNSResolver::kNullHandle;
272   absl::StatusOr<std::unique_ptr<
273       grpc_event_engine::experimental::EventEngine::DNSResolver>>
274       ee_resolver_;
275 };
276 
277 }  // namespace grpc_core
278 
279 #endif  // GRPC_SRC_CORE_UTIL_HTTP_CLIENT_HTTPCLI_H
280