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 <grpc/support/time.h> 27 28 #include "src/core/lib/http/parser.h" 29 #include "src/core/lib/iomgr/endpoint.h" 30 #include "src/core/lib/iomgr/iomgr_internal.h" 31 #include "src/core/lib/iomgr/polling_entity.h" 32 #include "src/core/lib/iomgr/pollset_set.h" 33 34 /* User agent this library reports */ 35 #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0" 36 37 /* Tracks in-progress http requests 38 TODO(ctiller): allow caching and capturing multiple requests for the 39 same content and combining them */ 40 typedef struct grpc_httpcli_context { 41 grpc_pollset_set* pollset_set; 42 } grpc_httpcli_context; 43 44 struct grpc_httpcli_handshaker { 45 const char* default_port; 46 void (*handshake)(void* arg, grpc_endpoint* endpoint, const char* host, 47 grpc_millis deadline, 48 void (*on_done)(void* arg, grpc_endpoint* endpoint)); 49 }; 50 extern const grpc_httpcli_handshaker grpc_httpcli_plaintext; 51 extern const grpc_httpcli_handshaker grpc_httpcli_ssl; 52 53 /* A request */ 54 typedef struct grpc_httpcli_request { 55 /* The host name to connect to */ 56 char* host; 57 /* The host to verify in the SSL handshake (or NULL) */ 58 char* ssl_host_override; 59 /* The main part of the request 60 The following headers are supplied automatically and MUST NOT be set here: 61 Host, Connection, User-Agent */ 62 grpc_http_request http; 63 /* handshaker to use ssl for the request */ 64 const grpc_httpcli_handshaker* handshaker; 65 } grpc_httpcli_request; 66 67 /* Expose the parser response type as a httpcli response too */ 68 typedef struct grpc_http_response grpc_httpcli_response; 69 70 void grpc_httpcli_context_init(grpc_httpcli_context* context); 71 void grpc_httpcli_context_destroy(grpc_httpcli_context* context); 72 73 /* Asynchronously perform a HTTP GET. 74 'context' specifies the http context under which to do the get 75 'pollset' indicates a grpc_pollset that is interested in the result 76 of the get - work on this pollset may be used to progress the get 77 operation 78 'request' contains request parameters - these are caller owned and can be 79 destroyed once the call returns 80 'deadline' contains a deadline for the request (or gpr_inf_future) 81 'on_response' is a callback to report results to */ 82 void grpc_httpcli_get(grpc_httpcli_context* context, 83 grpc_polling_entity* pollent, 84 grpc_resource_quota* resource_quota, 85 const grpc_httpcli_request* request, grpc_millis deadline, 86 grpc_closure* on_done, grpc_httpcli_response* response); 87 88 /* Asynchronously perform a HTTP POST. 89 'context' specifies the http context under which to do the post 90 'pollset' indicates a grpc_pollset that is interested in the result 91 of the post - work on this pollset may be used to progress the post 92 operation 93 'request' contains request parameters - these are caller owned and can be 94 destroyed once the call returns 95 'body_bytes' and 'body_size' specify the payload for the post. 96 When there is no body, pass in NULL as body_bytes. 97 'deadline' contains a deadline for the request (or gpr_inf_future) 98 'em' points to a caller owned event manager that must be alive for the 99 lifetime of the request 100 'on_response' is a callback to report results to 101 Does not support ?var1=val1&var2=val2 in the path. */ 102 void grpc_httpcli_post(grpc_httpcli_context* context, 103 grpc_polling_entity* pollent, 104 grpc_resource_quota* resource_quota, 105 const grpc_httpcli_request* request, 106 const char* body_bytes, size_t body_size, 107 grpc_millis deadline, grpc_closure* on_done, 108 grpc_httpcli_response* response); 109 110 /* override functions return 1 if they handled the request, 0 otherwise */ 111 typedef int (*grpc_httpcli_get_override)(const grpc_httpcli_request* request, 112 grpc_millis deadline, 113 grpc_closure* on_complete, 114 grpc_httpcli_response* response); 115 typedef int (*grpc_httpcli_post_override)(const grpc_httpcli_request* request, 116 const char* body_bytes, 117 size_t body_size, 118 grpc_millis deadline, 119 grpc_closure* on_complete, 120 grpc_httpcli_response* response); 121 122 void grpc_httpcli_set_override(grpc_httpcli_get_override get, 123 grpc_httpcli_post_override post); 124 125 #endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */ 126