blob: 320c0f86c61820edff738181ed2b9ef6e0c01242 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, 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
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/http/parser.h"
42#include "src/core/lib/iomgr/endpoint.h"
43#include "src/core/lib/iomgr/iomgr_internal.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070044#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/iomgr/pollset_set.h"
Craig Tiller00d5b5c2015-05-08 10:20:59 -070046
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047/* User agent this library reports */
48#define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
Craig Tillera0abe372015-06-01 11:31:08 -070050/* Tracks in-progress http requests
51 TODO(ctiller): allow caching and capturing multiple requests for the
52 same content and combining them */
Craig Tillera82950e2015-09-22 12:33:20 -070053typedef struct grpc_httpcli_context {
Craig Tiller69b093b2016-02-25 19:04:07 -080054 grpc_pollset_set *pollset_set;
Craig Tillera0abe372015-06-01 11:31:08 -070055} grpc_httpcli_context;
56
Craig Tillera82950e2015-09-22 12:33:20 -070057typedef struct {
Craig Tillerf53d9c82015-08-04 14:19:43 -070058 const char *default_port;
Craig Tillera82950e2015-09-22 12:33:20 -070059 void (*handshake)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint,
Craig Tiller449c64b2016-06-13 16:26:50 -070060 const char *host, gpr_timespec deadline,
Craig Tillera82950e2015-09-22 12:33:20 -070061 void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
62 grpc_endpoint *endpoint));
Craig Tillerf53d9c82015-08-04 14:19:43 -070063} grpc_httpcli_handshaker;
64
65extern const grpc_httpcli_handshaker grpc_httpcli_plaintext;
66extern const grpc_httpcli_handshaker grpc_httpcli_ssl;
67
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068/* A request */
Craig Tillera82950e2015-09-22 12:33:20 -070069typedef struct grpc_httpcli_request {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 /* The host name to connect to */
71 char *host;
Craig Tillercc0535d2015-12-08 15:14:47 -080072 /* The host to verify in the SSL handshake (or NULL) */
73 char *ssl_host_override;
Matthew Iselin1824f052016-02-10 12:16:06 +110074 /* The main part of the request
75 The following headers are supplied automatically and MUST NOT be set here:
Craig Tiller45724b32015-09-22 10:42:19 -070076 Host, Connection, User-Agent */
Matthew Iselin1824f052016-02-10 12:16:06 +110077 grpc_http_request http;
Craig Tillerf53d9c82015-08-04 14:19:43 -070078 /* handshaker to use ssl for the request */
79 const grpc_httpcli_handshaker *handshaker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080} grpc_httpcli_request;
81
Matthew Iselin1824f052016-02-10 12:16:06 +110082/* Expose the parser response type as a httpcli response too */
83typedef struct grpc_http_response grpc_httpcli_response;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084
Craig Tillera82950e2015-09-22 12:33:20 -070085void grpc_httpcli_context_init(grpc_httpcli_context *context);
86void grpc_httpcli_context_destroy(grpc_httpcli_context *context);
Craig Tillera0abe372015-06-01 11:31:08 -070087
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088/* Asynchronously perform a HTTP GET.
Craig Tiller6174b9a2015-06-18 08:13:05 -070089 'context' specifies the http context under which to do the get
90 'pollset' indicates a grpc_pollset that is interested in the result
91 of the get - work on this pollset may be used to progress the get
92 operation
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 'request' contains request parameters - these are caller owned and can be
94 destroyed once the call returns
95 'deadline' contains a deadline for the request (or gpr_inf_future)
Mark D. Rothf7250192016-07-22 08:06:09 -070096 'on_response' is a callback to report results to */
Craig Tillera82950e2015-09-22 12:33:20 -070097void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070098 grpc_polling_entity *pollent,
Craig Tillera82950e2015-09-22 12:33:20 -070099 const grpc_httpcli_request *request,
Craig Tiller5b15afd2016-05-04 15:00:14 -0700100 gpr_timespec deadline, grpc_closure *on_complete,
101 grpc_httpcli_response *response);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102
103/* Asynchronously perform a HTTP POST.
Craig Tiller6174b9a2015-06-18 08:13:05 -0700104 'context' specifies the http context under which to do the post
105 'pollset' indicates a grpc_pollset that is interested in the result
106 of the post - work on this pollset may be used to progress the post
107 operation
108 'request' contains request parameters - these are caller owned and can be
109 destroyed once the call returns
110 'body_bytes' and 'body_size' specify the payload for the post.
111 When there is no body, pass in NULL as body_bytes.
112 'deadline' contains a deadline for the request (or gpr_inf_future)
113 'em' points to a caller owned event manager that must be alive for the
114 lifetime of the request
Mark D. Rothf7250192016-07-22 08:06:09 -0700115 'on_response' is a callback to report results to
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 Does not support ?var1=val1&var2=val2 in the path. */
Craig Tillera82950e2015-09-22 12:33:20 -0700117void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700118 grpc_polling_entity *pollent,
Craig Tillera82950e2015-09-22 12:33:20 -0700119 const grpc_httpcli_request *request,
120 const char *body_bytes, size_t body_size,
Craig Tiller5b15afd2016-05-04 15:00:14 -0700121 gpr_timespec deadline, grpc_closure *on_complete,
122 grpc_httpcli_response *response);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123
ctiller40260c42014-12-17 17:03:15 -0800124/* override functions return 1 if they handled the request, 0 otherwise */
Craig Tillera82950e2015-09-22 12:33:20 -0700125typedef int (*grpc_httpcli_get_override)(grpc_exec_ctx *exec_ctx,
126 const grpc_httpcli_request *request,
127 gpr_timespec deadline,
Craig Tiller5b15afd2016-05-04 15:00:14 -0700128 grpc_closure *on_complete,
129 grpc_httpcli_response *response);
Craig Tillera82950e2015-09-22 12:33:20 -0700130typedef int (*grpc_httpcli_post_override)(
131 grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
132 const char *body_bytes, size_t body_size, gpr_timespec deadline,
Craig Tiller5b15afd2016-05-04 15:00:14 -0700133 grpc_closure *on_complete, grpc_httpcli_response *response);
ctiller40260c42014-12-17 17:03:15 -0800134
Craig Tillera82950e2015-09-22 12:33:20 -0700135void grpc_httpcli_set_override(grpc_httpcli_get_override get,
136 grpc_httpcli_post_override post);
ctiller40260c42014-12-17 17:03:15 -0800137
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700138#endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */