Home | History | Annotate | Download | only in http
      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 typedef struct {
     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 } grpc_httpcli_handshaker;
     50 
     51 extern const grpc_httpcli_handshaker grpc_httpcli_plaintext;
     52 extern const grpc_httpcli_handshaker grpc_httpcli_ssl;
     53 
     54 /* A request */
     55 typedef struct grpc_httpcli_request {
     56   /* The host name to connect to */
     57   char* host;
     58   /* The host to verify in the SSL handshake (or NULL) */
     59   char* ssl_host_override;
     60   /* The main part of the request
     61      The following headers are supplied automatically and MUST NOT be set here:
     62      Host, Connection, User-Agent */
     63   grpc_http_request http;
     64   /* handshaker to use ssl for the request */
     65   const grpc_httpcli_handshaker* handshaker;
     66 } grpc_httpcli_request;
     67 
     68 /* Expose the parser response type as a httpcli response too */
     69 typedef struct grpc_http_response grpc_httpcli_response;
     70 
     71 void grpc_httpcli_context_init(grpc_httpcli_context* context);
     72 void grpc_httpcli_context_destroy(grpc_httpcli_context* context);
     73 
     74 /* Asynchronously perform a HTTP GET.
     75    'context' specifies the http context under which to do the get
     76    'pollset' indicates a grpc_pollset that is interested in the result
     77      of the get - work on this pollset may be used to progress the get
     78      operation
     79    'request' contains request parameters - these are caller owned and can be
     80      destroyed once the call returns
     81    'deadline' contains a deadline for the request (or gpr_inf_future)
     82    'on_response' is a callback to report results to */
     83 void grpc_httpcli_get(grpc_httpcli_context* context,
     84                       grpc_polling_entity* pollent,
     85                       grpc_resource_quota* resource_quota,
     86                       const grpc_httpcli_request* request, grpc_millis deadline,
     87                       grpc_closure* on_complete,
     88                       grpc_httpcli_response* response);
     89 
     90 /* Asynchronously perform a HTTP POST.
     91    'context' specifies the http context under which to do the post
     92    'pollset' indicates a grpc_pollset that is interested in the result
     93      of the post - work on this pollset may be used to progress the post
     94      operation
     95    'request' contains request parameters - these are caller owned and can be
     96      destroyed once the call returns
     97    'body_bytes' and 'body_size' specify the payload for the post.
     98      When there is no body, pass in NULL as body_bytes.
     99    'deadline' contains a deadline for the request (or gpr_inf_future)
    100    'em' points to a caller owned event manager that must be alive for the
    101      lifetime of the request
    102    'on_response' is a callback to report results to
    103    Does not support ?var1=val1&var2=val2 in the path. */
    104 void grpc_httpcli_post(grpc_httpcli_context* context,
    105                        grpc_polling_entity* pollent,
    106                        grpc_resource_quota* resource_quota,
    107                        const grpc_httpcli_request* request,
    108                        const char* body_bytes, size_t body_size,
    109                        grpc_millis deadline, grpc_closure* on_complete,
    110                        grpc_httpcli_response* response);
    111 
    112 /* override functions return 1 if they handled the request, 0 otherwise */
    113 typedef int (*grpc_httpcli_get_override)(const grpc_httpcli_request* request,
    114                                          grpc_millis deadline,
    115                                          grpc_closure* on_complete,
    116                                          grpc_httpcli_response* response);
    117 typedef int (*grpc_httpcli_post_override)(const grpc_httpcli_request* request,
    118                                           const char* body_bytes,
    119                                           size_t body_size,
    120                                           grpc_millis deadline,
    121                                           grpc_closure* on_complete,
    122                                           grpc_httpcli_response* response);
    123 
    124 void grpc_httpcli_set_override(grpc_httpcli_get_override get,
    125                                grpc_httpcli_post_override post);
    126 
    127 #endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */
    128