Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 9d84fa8 | 2016-02-25 20:08:36 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_HTTP_HTTPCLI_H |
| 35 | #define GRPC_CORE_LIB_HTTP_HTTPCLI_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <stddef.h> |
| 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
| 40 | |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 41 | #include "src/core/http/parser.h" |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 42 | #include "src/core/iomgr/endpoint.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 43 | #include "src/core/iomgr/iomgr_internal.h" |
Craig Tiller | 00d5b5c | 2015-05-08 10:20:59 -0700 | [diff] [blame] | 44 | #include "src/core/iomgr/pollset_set.h" |
| 45 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | /* User agent this library reports */ |
| 47 | #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 49 | /* Tracks in-progress http requests |
| 50 | TODO(ctiller): allow caching and capturing multiple requests for the |
| 51 | same content and combining them */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | typedef struct grpc_httpcli_context { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 53 | grpc_pollset_set *pollset_set; |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 54 | } grpc_httpcli_context; |
| 55 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | typedef struct { |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 57 | const char *default_port; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | 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 Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 62 | } grpc_httpcli_handshaker; |
| 63 | |
| 64 | extern const grpc_httpcli_handshaker grpc_httpcli_plaintext; |
| 65 | extern const grpc_httpcli_handshaker grpc_httpcli_ssl; |
| 66 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | /* A request */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | typedef struct grpc_httpcli_request { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | /* The host name to connect to */ |
| 70 | char *host; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 71 | /* The host to verify in the SSL handshake (or NULL) */ |
| 72 | char *ssl_host_override; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 73 | /* The main part of the request |
| 74 | The following headers are supplied automatically and MUST NOT be set here: |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 75 | Host, Connection, User-Agent */ |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 76 | grpc_http_request http; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 77 | /* handshaker to use ssl for the request */ |
| 78 | const grpc_httpcli_handshaker *handshaker; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | } grpc_httpcli_request; |
| 80 | |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 81 | /* Expose the parser response type as a httpcli response too */ |
| 82 | typedef struct grpc_http_response grpc_httpcli_response; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | |
Julien Boeuf | feca1bf | 2015-06-22 16:46:20 +0200 | [diff] [blame] | 84 | /* Callback for grpc_httpcli_get and grpc_httpcli_post. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | typedef void (*grpc_httpcli_response_cb)(grpc_exec_ctx *exec_ctx, |
| 86 | void *user_data, |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 87 | const grpc_http_response *response); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | void grpc_httpcli_context_init(grpc_httpcli_context *context); |
| 90 | void grpc_httpcli_context_destroy(grpc_httpcli_context *context); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 91 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | /* Asynchronously perform a HTTP GET. |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 93 | '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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | '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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | 'on_response' is a callback to report results to (and 'user_data' is a user |
| 101 | supplied pointer to pass to said call) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | void 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | |
| 108 | /* Asynchronously perform a HTTP POST. |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 109 | '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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | Does not support ?var1=val1&var2=val2 in the path. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | void 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 130 | /* override functions return 1 if they handled the request, 0 otherwise */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | typedef 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); |
| 136 | typedef 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); |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 140 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | void grpc_httpcli_set_override(grpc_httpcli_get_override get, |
| 142 | grpc_httpcli_post_override post); |
ctiller | 40260c4 | 2014-12-17 17:03:15 -0800 | [diff] [blame] | 143 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 144 | #endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */ |