httpcli.h
Go to the documentation of this file.
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_CORE_LIB_HTTP_HTTPCLI_H
20 #define GRPC_CORE_LIB_HTTP_HTTPCLI_H
21 
23 
24 #include <stddef.h>
25 
26 #include <functional>
27 #include <vector>
28 
29 #include "absl/base/thread_annotations.h"
30 #include "absl/status/statusor.h"
31 #include "absl/types/optional.h"
32 
33 #include <grpc/grpc.h>
35 #include <grpc/slice.h>
36 
55 
56 /* User agent this library reports */
57 #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
58 
59 /* override functions return 1 if they handled the request, 0 otherwise */
61  const char* host, const char* path,
62  grpc_core::Timestamp deadline,
63  grpc_closure* on_complete,
66  const grpc_http_request* request, const char* host, const char* path,
67  const char* body_bytes, size_t body_size, grpc_core::Timestamp deadline,
70  const grpc_http_request* request, const char* host, const char* path,
71  const char* body_bytes, size_t body_size, grpc_core::Timestamp deadline,
73 
74 namespace grpc_core {
75 
76 // Tracks an in-progress GET or POST request. Calling \a Start()
77 // begins async work and calling \a Orphan() arranges for async work
78 // to be completed as sooon as possible (possibly aborting the request
79 // if it's in flight).
80 // TODO(ctiller): allow caching and capturing multiple requests for the
81 // same content and combining them
82 class HttpRequest : public InternallyRefCounted<HttpRequest> {
83  public:
84  // Asynchronously perform a HTTP GET.
85  // 'uri' is the target to make the request to. The scheme field is used to
86  // determine the port number. The authority field is the target host. The
87  // path field determines the path of the request. No other fields are used.
88  // 'args' are optional channel args for the request.
89  // 'pollent' indicates a grpc_polling_entity that is interested in the result
90  // of the get - work on this entity may be used to progress the get
91  // operation
92  // 'request' contains request parameters - these are caller owned and
93  // can be destroyed once the call returns
94  // 'deadline' contains a deadline for the request (or gpr_inf_future)
95  // 'on_done' is a callback to report results to
96  // 'channel_creds' are used to configurably secure the connection.
97  // For insecure requests, use grpc_insecure_credentials_create.
98  // For secure requests, use CreateHttpRequestSSLCredentials().
99  // nullptr is treated as insecure credentials.
100  // TODO(yihuaz): disallow nullptr as a value after unsecure builds
101  // are removed.
103  URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
104  const grpc_http_request* request, Timestamp deadline,
108 
109  // Asynchronously perform a HTTP POST.
110  // 'uri' is the target to make the request to. The scheme field is used to
111  // determine the port number. The authority field is the target host. The
112  // path field determines the path of the request. No other fields are used.
113  // 'args' are optional channel args for the request.
114  // 'pollent' indicates a grpc_polling_entity that is interested in the result
115  // of the post - work on this entity may be used to progress the post
116  // operation
117  // 'request' contains request parameters - these are caller owned and can be
118  // destroyed once the call returns
119  // 'deadline' contains a deadline for the request (or gpr_inf_future)
120  // 'on_done' is a callback to report results to
121  // 'channel_creds' are used to configurably secure the connection.
122  // For insecure requests, use grpc_insecure_credentials_create.
123  // For secure requests, use CreateHttpRequestSSLCredentials().
124  // nullptr is treated as insecure credentials.
125  // TODO(apolcyn): disallow nullptr as a value after unsecure builds
126  // are removed.
127  // Does not support ?var1=val1&var2=val2 in the path.
129  URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
130  const grpc_http_request* request, Timestamp deadline,
134 
135  // Asynchronously perform a HTTP PUT.
136  // 'uri' is the target to make the request to. The scheme field is used to
137  // determine the port number. The authority field is the target host. The
138  // path field determines the path of the request. No other fields are used.
139  // 'args' are optional channel args for the request.
140  // 'pollent' indicates a grpc_polling_entity that is interested in the result
141  // of the post - work on this entity may be used to progress the post
142  // operation
143  // 'request' contains request parameters - these are caller owned and can be
144  // destroyed once the call returns
145  // 'deadline' contains a deadline for the request (or gpr_inf_future)
146  // 'on_done' is a callback to report results to
147  // 'channel_creds' are used to configurably secure the connection.
148  // For insecure requests, use grpc_insecure_credentials_create.
149  // For secure requests, use CreateHttpRequestSSLCredentials().
150  // nullptr is treated as insecure credentials.
151  // TODO(apolcyn): disallow nullptr as a value after unsecure builds
152  // are removed.
153  // Does not support ?var1=val1&var2=val2 in the path.
155  URI uri, const grpc_channel_args* args, grpc_polling_entity* pollent,
156  const grpc_http_request* request, Timestamp deadline,
160 
161  HttpRequest(URI uri, const grpc_slice& request_text,
163  const grpc_channel_args* channel_args, grpc_closure* on_done,
164  grpc_polling_entity* pollent, const char* name,
165  absl::optional<std::function<void()>> test_only_generate_response,
167 
168  ~HttpRequest() override;
169 
170  void Start();
171 
172  void Orphan() override;
173 
177 
179  void (*intercept)(HttpRequest* req));
180 
181  private:
185  }
186 
188 
190  Ref().release(); // ref held by pending read
191  grpc_endpoint_read(ep_, &incoming_, &on_read_, /*urgent=*/true,
192  /*min_progress_size=*/1);
193  }
194 
195  static void OnRead(void* user_data, grpc_error_handle error) {
196  HttpRequest* req = static_cast<HttpRequest*>(user_data);
198  &req->continue_on_read_after_schedule_on_exec_ctx_,
200  }
201 
202  // Needed since OnRead may be called inline from grpc_endpoint_read
203  static void ContinueOnReadAfterScheduleOnExecCtx(void* user_data,
205  RefCountedPtr<HttpRequest> req(static_cast<HttpRequest*>(user_data));
206  MutexLock lock(&req->mu_);
207  req->OnReadInternal(error);
208  }
209 
212 
214 
215  static void DoneWrite(void* arg, grpc_error_handle error) {
216  HttpRequest* req = static_cast<HttpRequest*>(arg);
218  &req->continue_done_write_after_schedule_on_exec_ctx_,
220  }
221 
222  // Needed since DoneWrite may be called inline from grpc_endpoint_write
225 
227 
228  static void OnHandshakeDone(void* arg, grpc_error_handle error);
229 
232 
234 
235  void OnResolved(
236  absl::StatusOr<std::vector<grpc_resolved_address>> addresses_or);
237 
247  grpc_endpoint* ep_ = nullptr;
255  bool own_endpoint_ ABSL_GUARDED_BY(mu_) = true;
256  bool cancelled_ ABSL_GUARDED_BY(mu_) = false;
259  size_t next_address_ ABSL_GUARDED_BY(mu_) = 0;
260  int have_read_byte_ ABSL_GUARDED_BY(mu_) = 0;
265  absl::optional<DNSResolver::TaskHandle> dns_request_handle_
266  ABSL_GUARDED_BY(mu_) = DNSResolver::kNullHandle;
267 };
268 
269 } // namespace grpc_core
270 
271 #endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */
grpc_core::HttpRequest::deadline_
const Timestamp deadline_
Definition: httpcli.h:240
grpc_core::HttpRequest::done_write_
grpc_closure done_write_
Definition: httpcli.h:245
grpc_core::HttpRequest::Orphan
void Orphan() override
Definition: httpcli.cc:217
grpc_core::HttpRequest::on_done_
grpc_closure * on_done_
Definition: httpcli.h:248
orphanable.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
grpc_core::HttpRequest::ContinueOnReadAfterScheduleOnExecCtx
static void ContinueOnReadAfterScheduleOnExecCtx(void *user_data, grpc_error_handle error)
Definition: httpcli.h:203
get
absl::string_view get(const Cont &c)
Definition: abseil-cpp/absl/strings/str_replace_test.cc:185
grpc_core::HttpRequest::OnReadInternal
void OnReadInternal(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.cc:256
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
grpc_core::HttpRequest::NextAddress
void NextAddress(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.cc:372
polling_entity.h
iomgr_internal.h
grpc_core::HttpRequest::continue_done_write_after_schedule_on_exec_ctx_
grpc_closure continue_done_write_after_schedule_on_exec_ctx_
Definition: httpcli.h:246
post
static void post(QUEUE *q, enum uv__work_kind kind)
Definition: threadpool.c:142
slice.h
false
#define false
Definition: setup_once.h:323
absl::StatusOr
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT StatusOr
Definition: abseil-cpp/absl/status/internal/statusor_internal.h:29
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
grpc_core::DNSResolver
Definition: resolve_address.h:42
benchmark.request
request
Definition: benchmark.py:77
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_endpoint_read
void grpc_endpoint_read(grpc_endpoint *ep, grpc_slice_buffer *slices, grpc_closure *cb, bool urgent, int min_progress_size)
Definition: endpoint.cc:25
grpc_core::HttpRequest::ContinueDoneWriteAfterScheduleOnExecCtx
static void ContinueDoneWriteAfterScheduleOnExecCtx(void *arg, grpc_error_handle error)
Definition: httpcli.cc:280
resolve_address.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::URI
Definition: uri_parser.h:31
addresses_
absl::StatusOr< HierarchicalAddressMap > addresses_
Definition: priority.cc:287
grpc_core::HttpRequest::DoHandshake
void DoHandshake(const grpc_resolved_address *addr) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.cc:328
grpc_resolved_address
Definition: resolved_address.h:34
closure.h
grpc_core::HttpRequest::continue_on_read_after_schedule_on_exec_ctx_
grpc_closure continue_on_read_after_schedule_on_exec_ctx_
Definition: httpcli.h:244
grpc_core::HttpRequest
Definition: httpcli.h:82
grpc_core::HttpRequest::OnWritten
void OnWritten() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.h:213
setup.name
name
Definition: setup.py:542
check_documentation.path
path
Definition: check_documentation.py:57
grpc_core::HttpRequest::Post
static OrphanablePtr< HttpRequest > Post(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
Definition: httpcli.cc:95
resolved_address.h
grpc_channel_args
Definition: grpc_types.h:132
grpc_httpcli_post_override
int(* grpc_httpcli_post_override)(const grpc_http_request *request, const char *host, const char *path, const char *body_bytes, size_t body_size, grpc_core::Timestamp deadline, grpc_closure *on_complete, grpc_http_response *response)
Definition: httpcli.h:65
true
#define true
Definition: setup_once.h:324
grpc_types.h
grpc_core::HttpRequest::on_read_
grpc_closure on_read_
Definition: httpcli.h:243
grpc_core::HttpRequest::SetOverride
static void SetOverride(grpc_httpcli_get_override get, grpc_httpcli_post_override post, grpc_httpcli_put_override put)
Definition: httpcli.cc:143
grpc_core::HttpRequest::AppendError
void AppendError(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.cc:242
grpc_iomgr_object
Definition: iomgr_internal.h:29
grpc_core::HttpRequest::test_only_generate_response_
const absl::optional< std::function< void()> > test_only_generate_response_
Definition: httpcli.h:252
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_http_response
Definition: src/core/lib/http/parser.h:85
grpc_core::HttpRequest::DoneWrite
static void DoneWrite(void *arg, grpc_error_handle error)
Definition: httpcli.h:215
grpc_core::HttpRequest::OnResolved
void OnResolved(absl::StatusOr< std::vector< grpc_resolved_address >> addresses_or)
Definition: httpcli.cc:390
grpc_core::HttpRequest::channel_creds_
RefCountedPtr< grpc_channel_credentials > channel_creds_
Definition: httpcli.h:242
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
xds_interop_client.int
int
Definition: xds_interop_client.py:113
grpc_core::RefCountedPtr< grpc_channel_credentials >
grpc_core::HttpRequest::HttpRequest
HttpRequest(URI uri, const grpc_slice &request_text, grpc_http_response *response, Timestamp deadline, const grpc_channel_args *channel_args, grpc_closure *on_done, grpc_polling_entity *pollent, const char *name, absl::optional< std::function< void()>> test_only_generate_response, RefCountedPtr< grpc_channel_credentials > channel_creds)
Definition: httpcli.cc:156
grpc_httpcli_put_override
int(* grpc_httpcli_put_override)(const grpc_http_request *request, const char *host, const char *path, const char *body_bytes, size_t body_size, grpc_core::Timestamp deadline, grpc_closure *on_complete, grpc_http_response *response)
Definition: httpcli.h:69
grpc_core::HttpRequest::DoRead
void DoRead() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.h:189
req
static uv_connect_t req
Definition: test-connection-fail.c:30
grpc_core::InternallyRefCounted< HttpRequest >::Ref
RefCountedPtr< HttpRequest > Ref() GRPC_MUST_USE_RESULT
Definition: orphanable.h:90
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
grpc.h
grpc_core::HttpRequest::OnRead
static void OnRead(void *user_data, grpc_error_handle error)
Definition: httpcli.h:195
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_polling_entity_del_from_pollset_set
void grpc_polling_entity_del_from_pollset_set(grpc_polling_entity *pollent, grpc_pollset_set *pss_dst)
Definition: polling_entity.cc:78
grpc_core::HttpRequest::resource_quota_
ResourceQuotaRefPtr resource_quota_
Definition: httpcli.h:249
arg
Definition: cmdline.cc:40
time.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
error.h
grpc_polling_entity
Definition: polling_entity.h:38
grpc_core::InternallyRefCounted
Definition: orphanable.h:73
outgoing_
grpc_slice_buffer outgoing_
Definition: security_handshaker.cc:128
grpc_core::HttpRequest::channel_args_
const grpc_channel_args * channel_args_
Definition: httpcli.h:241
resource_quota.h
grpc_core::HttpRequest::ep_
grpc_endpoint * ep_
Definition: httpcli.h:247
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
parser.h
GRPC_ERROR_REF
#define GRPC_ERROR_REF(err)
Definition: error.h:261
grpc_httpcli_get_override
int(* grpc_httpcli_get_override)(const grpc_http_request *request, const char *host, const char *path, grpc_core::Timestamp deadline, grpc_closure *on_complete, grpc_http_response *response)
Definition: httpcli.h:60
grpc_core::HttpRequest::Get
static OrphanablePtr< HttpRequest > Get(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
Definition: httpcli.cc:69
debug_location.h
GRPC_MUST_USE_RESULT
#define GRPC_MUST_USE_RESULT
Definition: impl/codegen/port_platform.h:584
grpc_core::HttpRequest::OnHandshakeDone
static void OnHandshakeDone(void *arg, grpc_error_handle error)
Definition: httpcli.cc:299
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::HttpRequest::Finish
void Finish(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.h:182
arg
struct arg arg
exec_ctx.h
handshaker.h
grpc_core::HttpRequest::ABSL_GUARDED_BY
RefCountedPtr< HandshakeManager > handshake_mgr_ ABSL_GUARDED_BY(mu_)
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
ref_counted_ptr.h
grpc_core::HttpRequest::uri_
const URI uri_
Definition: httpcli.h:238
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
grpc_core::HttpRequest::pollset_set_
grpc_pollset_set * pollset_set_
Definition: httpcli.h:251
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
grpc_core::HandshakeManager
Definition: handshaker.h:98
uri_parser.h
iomgr_fwd.h
grpc_core::HttpRequest::Put
static OrphanablePtr< HttpRequest > Put(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
Definition: httpcli.cc:119
endpoint.h
grpc_error
Definition: error_internal.h:42
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::HttpRequest::~HttpRequest
~HttpRequest() override
Definition: httpcli.cc:191
grpc_core::HttpRequest::request_text_
const grpc_slice request_text_
Definition: httpcli.h:239
parser_
std::unique_ptr< Parser > parser_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:185
grpc_core::HttpRequest::mu_
Mutex mu_
Definition: httpcli.h:253
grpc_http_parser
Definition: src/core/lib/http/parser.h:99
grpc_closure
Definition: closure.h:56
grpc_endpoint
Definition: endpoint.h:105
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
grpc_core::HttpRequest::Start
void Start()
Definition: httpcli.cc:205
grpc_core::HttpRequest::TestOnlySetOnHandshakeDoneIntercept
static void TestOnlySetOnHandshakeDoneIntercept(void(*intercept)(HttpRequest *req))
Definition: httpcli.cc:151
sync.h
grpc_core::HttpRequest::StartWrite
void StartWrite() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: httpcli.cc:291
grpc_core::HttpRequest::pollent_
grpc_polling_entity * pollent_
Definition: httpcli.h:250
grpc_http_request
Definition: src/core/lib/http/parser.h:69
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:14