blob: f28d6d7481ea36aab6c755e0436d60a364be6964 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller9d84fa82016-02-25 20:08:36 -08003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_HTTP_HTTPCLI_H
35#define GRPC_CORE_LIB_HTTP_HTTPCLI_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <stddef.h>
38
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/time.h>
40
Matthew Iselin1824f052016-02-10 12:16:06 +110041#include "src/core/http/parser.h"
Craig Tillerf53d9c82015-08-04 14:19:43 -070042#include "src/core/iomgr/endpoint.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080043#include "src/core/iomgr/iomgr_internal.h"
Craig Tiller00d5b5c2015-05-08 10:20:59 -070044#include "src/core/iomgr/pollset_set.h"
45
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046/* User agent this library reports */
47#define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Craig Tillera0abe372015-06-01 11:31:08 -070049/* Tracks in-progress http requests
50 TODO(ctiller): allow caching and capturing multiple requests for the
51 same content and combining them */
Craig Tillera82950e2015-09-22 12:33:20 -070052typedef struct grpc_httpcli_context {
Craig Tiller69b093b2016-02-25 19:04:07 -080053 grpc_pollset_set *pollset_set;
Craig Tillera0abe372015-06-01 11:31:08 -070054} grpc_httpcli_context;
55
Craig Tillera82950e2015-09-22 12:33:20 -070056typedef struct {
Craig Tillerf53d9c82015-08-04 14:19:43 -070057 const char *default_port;
Craig Tillera82950e2015-09-22 12:33:20 -070058 void (*handshake)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint,
59 const char *host,
60 void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
61 grpc_endpoint *endpoint));
Craig Tillerf53d9c82015-08-04 14:19:43 -070062} grpc_httpcli_handshaker;
63
64extern const grpc_httpcli_handshaker grpc_httpcli_plaintext;
65extern const grpc_httpcli_handshaker grpc_httpcli_ssl;
66
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067/* A request */
Craig Tillera82950e2015-09-22 12:33:20 -070068typedef struct grpc_httpcli_request {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 /* The host name to connect to */
70 char *host;
Craig Tillercc0535d2015-12-08 15:14:47 -080071 /* The host to verify in the SSL handshake (or NULL) */
72 char *ssl_host_override;
Matthew Iselin1824f052016-02-10 12:16:06 +110073 /* The main part of the request
74 The following headers are supplied automatically and MUST NOT be set here:
Craig Tiller45724b32015-09-22 10:42:19 -070075 Host, Connection, User-Agent */
Matthew Iselin1824f052016-02-10 12:16:06 +110076 grpc_http_request http;
Craig Tillerf53d9c82015-08-04 14:19:43 -070077 /* handshaker to use ssl for the request */
78 const grpc_httpcli_handshaker *handshaker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079} grpc_httpcli_request;
80
Matthew Iselin1824f052016-02-10 12:16:06 +110081/* Expose the parser response type as a httpcli response too */
82typedef struct grpc_http_response grpc_httpcli_response;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083
Julien Boeuffeca1bf2015-06-22 16:46:20 +020084/* Callback for grpc_httpcli_get and grpc_httpcli_post. */
Craig Tillera82950e2015-09-22 12:33:20 -070085typedef void (*grpc_httpcli_response_cb)(grpc_exec_ctx *exec_ctx,
86 void *user_data,
Matthew Iselin1824f052016-02-10 12:16:06 +110087 const grpc_http_response *response);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
Craig Tillera82950e2015-09-22 12:33:20 -070089void grpc_httpcli_context_init(grpc_httpcli_context *context);
90void grpc_httpcli_context_destroy(grpc_httpcli_context *context);
Craig Tillera0abe372015-06-01 11:31:08 -070091
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092/* Asynchronously perform a HTTP GET.
Craig Tiller6174b9a2015-06-18 08:13:05 -070093 'context' specifies the http context under which to do the get
94 'pollset' indicates a grpc_pollset that is interested in the result
95 of the get - work on this pollset may be used to progress the get
96 operation
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097 'request' contains request parameters - these are caller owned and can be
98 destroyed once the call returns
99 'deadline' contains a deadline for the request (or gpr_inf_future)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100 'on_response' is a callback to report results to (and 'user_data' is a user
101 supplied pointer to pass to said call) */
Craig Tillera82950e2015-09-22 12:33:20 -0700102void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
103 grpc_pollset *pollset,
104 const grpc_httpcli_request *request,
105 gpr_timespec deadline,
106 grpc_httpcli_response_cb on_response, void *user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107
108/* Asynchronously perform a HTTP POST.
Craig Tiller6174b9a2015-06-18 08:13:05 -0700109 'context' specifies the http context under which to do the post
110 'pollset' indicates a grpc_pollset that is interested in the result
111 of the post - work on this pollset may be used to progress the post
112 operation
113 'request' contains request parameters - these are caller owned and can be
114 destroyed once the call returns
115 'body_bytes' and 'body_size' specify the payload for the post.
116 When there is no body, pass in NULL as body_bytes.
117 'deadline' contains a deadline for the request (or gpr_inf_future)
118 'em' points to a caller owned event manager that must be alive for the
119 lifetime of the request
120 'on_response' is a callback to report results to (and 'user_data' is a user
121 supplied pointer to pass to said call)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122 Does not support ?var1=val1&var2=val2 in the path. */
Craig Tillera82950e2015-09-22 12:33:20 -0700123void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
124 grpc_pollset *pollset,
125 const grpc_httpcli_request *request,
126 const char *body_bytes, size_t body_size,
127 gpr_timespec deadline,
128 grpc_httpcli_response_cb on_response, void *user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129
ctiller40260c42014-12-17 17:03:15 -0800130/* override functions return 1 if they handled the request, 0 otherwise */
Craig Tillera82950e2015-09-22 12:33:20 -0700131typedef int (*grpc_httpcli_get_override)(grpc_exec_ctx *exec_ctx,
132 const grpc_httpcli_request *request,
133 gpr_timespec deadline,
134 grpc_httpcli_response_cb on_response,
135 void *user_data);
136typedef int (*grpc_httpcli_post_override)(
137 grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
138 const char *body_bytes, size_t body_size, gpr_timespec deadline,
139 grpc_httpcli_response_cb on_response, void *user_data);
ctiller40260c42014-12-17 17:03:15 -0800140
Craig Tillera82950e2015-09-22 12:33:20 -0700141void grpc_httpcli_set_override(grpc_httpcli_get_override get,
142 grpc_httpcli_post_override post);
ctiller40260c42014-12-17 17:03:15 -0800143
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700144#endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */