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