mirror of https://github.com/grpc/grpc.git
commit
995bc78441
162 changed files with 2940 additions and 4741 deletions
@ -0,0 +1,8 @@ |
||||
-Ithird_party/googletest/include |
||||
-Ithird_party/googletest |
||||
-Iinclude |
||||
-Igens |
||||
-I. |
||||
-Ithird_party/boringssl/include |
||||
-Ithird_party/zlib |
||||
-Ithird_party/protobuf/src |
@ -1,14 +1,28 @@ |
||||
# Top-level Items by language |
||||
|
||||
## Bazel |
||||
* [grpc.bzl](grpc.bzl) |
||||
|
||||
## Node |
||||
* [binding.gyp](binding.gyp) |
||||
* [package.json](package.json) |
||||
|
||||
## Objective-C |
||||
* [gRPC.podspec](gRPC.podspec) |
||||
|
||||
## PHP |
||||
* [composer.json](composer.json) |
||||
* [config.m4](config.m4) |
||||
* [package.xml](package.xml) |
||||
|
||||
## Python |
||||
* [requirements.txt](requirements.txt) |
||||
* [setup.cfg](setup.cfg) |
||||
* [setup.py](setup.py) |
||||
* [tox.ini](tox.ini) |
||||
* [PYTHON-MANIFEST.in](PYTHON-MANIFEST.in) |
||||
|
||||
## Ruby |
||||
* [Gemfile](Gemfile) |
||||
* [grpc.gemspec](grpc.gemspec) |
||||
* [Rakefile](Rakefile) |
||||
|
@ -1,233 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/channel/client_uchannel.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "src/core/census/grpc_filter.h" |
||||
#include "src/core/channel/channel_args.h" |
||||
#include "src/core/channel/client_channel.h" |
||||
#include "src/core/channel/compress_filter.h" |
||||
#include "src/core/channel/subchannel_call_holder.h" |
||||
#include "src/core/iomgr/iomgr.h" |
||||
#include "src/core/support/string.h" |
||||
#include "src/core/surface/channel.h" |
||||
#include "src/core/transport/connectivity_state.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/sync.h> |
||||
#include <grpc/support/useful.h> |
||||
|
||||
/** Microchannel (uchannel) implementation: a lightweight channel without any
|
||||
* load-balancing mechanisms meant for communication from within the core. */ |
||||
|
||||
typedef struct client_uchannel_channel_data { |
||||
/** master channel - the grpc_channel instance that ultimately owns
|
||||
this channel_data via its channel stack. |
||||
We occasionally use this to bump the refcount on the master channel |
||||
to keep ourselves alive through an asynchronous operation. */ |
||||
grpc_channel_stack *owning_stack; |
||||
|
||||
/** connectivity state being tracked */ |
||||
grpc_connectivity_state_tracker state_tracker; |
||||
|
||||
/** the subchannel wrapped by the microchannel */ |
||||
grpc_connected_subchannel *connected_subchannel; |
||||
|
||||
/** the callback used to stay subscribed to subchannel connectivity
|
||||
* notifications */ |
||||
grpc_closure connectivity_cb; |
||||
|
||||
/** the current connectivity state of the wrapped subchannel */ |
||||
grpc_connectivity_state subchannel_connectivity; |
||||
|
||||
gpr_mu mu_state; |
||||
} channel_data; |
||||
|
||||
typedef grpc_subchannel_call_holder call_data; |
||||
|
||||
static void monitor_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
||||
bool iomgr_success) { |
||||
channel_data *chand = arg; |
||||
grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
||||
chand->subchannel_connectivity, |
||||
"uchannel_monitor_subchannel"); |
||||
grpc_connected_subchannel_notify_on_state_change( |
||||
exec_ctx, chand->connected_subchannel, NULL, |
||||
&chand->subchannel_connectivity, &chand->connectivity_cb); |
||||
} |
||||
|
||||
static char *cuc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
||||
return grpc_subchannel_call_holder_get_peer(exec_ctx, elem->call_data); |
||||
} |
||||
|
||||
static void cuc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
||||
grpc_call_element *elem, |
||||
grpc_transport_stream_op *op) { |
||||
GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
||||
grpc_subchannel_call_holder_perform_op(exec_ctx, elem->call_data, op); |
||||
} |
||||
|
||||
static void cuc_start_transport_op(grpc_exec_ctx *exec_ctx, |
||||
grpc_channel_element *elem, |
||||
grpc_transport_op *op) { |
||||
channel_data *chand = elem->channel_data; |
||||
|
||||
grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, true, NULL); |
||||
|
||||
GPR_ASSERT(op->set_accept_stream == false); |
||||
GPR_ASSERT(op->bind_pollset == NULL); |
||||
|
||||
if (op->on_connectivity_state_change != NULL) { |
||||
grpc_connectivity_state_notify_on_state_change( |
||||
exec_ctx, &chand->state_tracker, op->connectivity_state, |
||||
op->on_connectivity_state_change); |
||||
op->on_connectivity_state_change = NULL; |
||||
op->connectivity_state = NULL; |
||||
} |
||||
|
||||
if (op->disconnect) { |
||||
grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
||||
GRPC_CHANNEL_FATAL_FAILURE, "disconnect"); |
||||
} |
||||
} |
||||
|
||||
static int cuc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_metadata_batch *initial_metadata, |
||||
grpc_connected_subchannel **connected_subchannel, |
||||
grpc_closure *on_ready) { |
||||
channel_data *chand = arg; |
||||
GPR_ASSERT(initial_metadata != NULL); |
||||
*connected_subchannel = chand->connected_subchannel; |
||||
return 1; |
||||
} |
||||
|
||||
/* Constructor for call_data */ |
||||
static void cuc_init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
||||
grpc_call_element_args *args) { |
||||
grpc_subchannel_call_holder_init(elem->call_data, cuc_pick_subchannel, |
||||
elem->channel_data, args->call_stack); |
||||
} |
||||
|
||||
/* Destructor for call_data */ |
||||
static void cuc_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
||||
grpc_call_element *elem) { |
||||
grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data); |
||||
} |
||||
|
||||
/* Constructor for channel_data */ |
||||
static void cuc_init_channel_elem(grpc_exec_ctx *exec_ctx, |
||||
grpc_channel_element *elem, |
||||
grpc_channel_element_args *args) { |
||||
channel_data *chand = elem->channel_data; |
||||
memset(chand, 0, sizeof(*chand)); |
||||
grpc_closure_init(&chand->connectivity_cb, monitor_subchannel, chand); |
||||
GPR_ASSERT(args->is_last); |
||||
GPR_ASSERT(elem->filter == &grpc_client_uchannel_filter); |
||||
chand->owning_stack = args->channel_stack; |
||||
grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
||||
"client_uchannel"); |
||||
gpr_mu_init(&chand->mu_state); |
||||
} |
||||
|
||||
/* Destructor for channel_data */ |
||||
static void cuc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
||||
grpc_channel_element *elem) { |
||||
channel_data *chand = elem->channel_data; |
||||
/* cancel subscription */ |
||||
grpc_connected_subchannel_notify_on_state_change( |
||||
exec_ctx, chand->connected_subchannel, NULL, NULL, |
||||
&chand->connectivity_cb); |
||||
grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
||||
gpr_mu_destroy(&chand->mu_state); |
||||
GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, chand->connected_subchannel, |
||||
"uchannel"); |
||||
} |
||||
|
||||
static void cuc_set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
||||
grpc_pollset *pollset) { |
||||
call_data *calld = elem->call_data; |
||||
calld->pollset = pollset; |
||||
} |
||||
|
||||
const grpc_channel_filter grpc_client_uchannel_filter = { |
||||
cuc_start_transport_stream_op, cuc_start_transport_op, sizeof(call_data), |
||||
cuc_init_call_elem, cuc_set_pollset, cuc_destroy_call_elem, |
||||
sizeof(channel_data), cuc_init_channel_elem, cuc_destroy_channel_elem, |
||||
cuc_get_peer, "client-uchannel", |
||||
}; |
||||
|
||||
grpc_connectivity_state grpc_client_uchannel_check_connectivity_state( |
||||
grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) { |
||||
channel_data *chand = elem->channel_data; |
||||
grpc_connectivity_state out; |
||||
gpr_mu_lock(&chand->mu_state); |
||||
out = grpc_connectivity_state_check(&chand->state_tracker); |
||||
gpr_mu_unlock(&chand->mu_state); |
||||
return out; |
||||
} |
||||
|
||||
void grpc_client_uchannel_watch_connectivity_state( |
||||
grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
||||
grpc_connectivity_state *state, grpc_closure *on_complete) { |
||||
channel_data *chand = elem->channel_data; |
||||
gpr_mu_lock(&chand->mu_state); |
||||
grpc_connectivity_state_notify_on_state_change( |
||||
exec_ctx, &chand->state_tracker, state, on_complete); |
||||
gpr_mu_unlock(&chand->mu_state); |
||||
} |
||||
|
||||
grpc_channel *grpc_client_uchannel_create(grpc_subchannel *subchannel, |
||||
grpc_channel_args *args) { |
||||
grpc_channel *channel = NULL; |
||||
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
|
||||
channel = |
||||
grpc_channel_create(&exec_ctx, NULL, args, GRPC_CLIENT_UCHANNEL, NULL); |
||||
|
||||
return channel; |
||||
} |
||||
|
||||
void grpc_client_uchannel_set_connected_subchannel( |
||||
grpc_channel *uchannel, grpc_connected_subchannel *connected_subchannel) { |
||||
grpc_channel_element *elem = |
||||
grpc_channel_stack_last_element(grpc_channel_get_channel_stack(uchannel)); |
||||
channel_data *chand = elem->channel_data; |
||||
GPR_ASSERT(elem->filter == &grpc_client_uchannel_filter); |
||||
gpr_mu_lock(&chand->mu_state); |
||||
chand->connected_subchannel = connected_subchannel; |
||||
GRPC_CONNECTED_SUBCHANNEL_REF(connected_subchannel, "uchannel"); |
||||
gpr_mu_unlock(&chand->mu_state); |
||||
} |
@ -0,0 +1,313 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/http/parser.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/useful.h> |
||||
|
||||
static char *buf2str(void *buffer, size_t length) { |
||||
char *out = gpr_malloc(length + 1); |
||||
memcpy(out, buffer, length); |
||||
out[length] = 0; |
||||
return out; |
||||
} |
||||
|
||||
static int handle_response_line(grpc_http_parser *parser) { |
||||
uint8_t *beg = parser->cur_line; |
||||
uint8_t *cur = beg; |
||||
uint8_t *end = beg + parser->cur_line_length; |
||||
|
||||
if (cur == end || *cur++ != 'H') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'P') goto error; |
||||
if (cur == end || *cur++ != '/') goto error; |
||||
if (cur == end || *cur++ != '1') goto error; |
||||
if (cur == end || *cur++ != '.') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '1') goto error; |
||||
if (cur == end || *cur++ != ' ') goto error; |
||||
if (cur == end || *cur < '1' || *cur++ > '9') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
||||
parser->http.response.status = |
||||
(cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0'); |
||||
if (cur == end || *cur++ != ' ') goto error; |
||||
|
||||
/* we don't really care about the status code message */ |
||||
|
||||
return 1; |
||||
|
||||
error: |
||||
gpr_log(GPR_ERROR, "Failed parsing response line"); |
||||
return 0; |
||||
} |
||||
|
||||
static int handle_request_line(grpc_http_parser *parser) { |
||||
uint8_t *beg = parser->cur_line; |
||||
uint8_t *cur = beg; |
||||
uint8_t *end = beg + parser->cur_line_length; |
||||
uint8_t vers_major = 0; |
||||
uint8_t vers_minor = 0; |
||||
|
||||
while (cur != end && *cur++ != ' ') |
||||
; |
||||
if (cur == end) goto error; |
||||
parser->http.request.method = buf2str(beg, (size_t)(cur - beg - 1)); |
||||
|
||||
beg = cur; |
||||
while (cur != end && *cur++ != ' ') |
||||
; |
||||
if (cur == end) goto error; |
||||
parser->http.request.path = buf2str(beg, (size_t)(cur - beg - 1)); |
||||
|
||||
if (cur == end || *cur++ != 'H') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'P') goto error; |
||||
if (cur == end || *cur++ != '/') goto error; |
||||
vers_major = (uint8_t)(*cur++ - '1' + 1); |
||||
++cur; |
||||
if (cur == end) goto error; |
||||
vers_minor = (uint8_t)(*cur++ - '1' + 1); |
||||
|
||||
if (vers_major == 1) { |
||||
if (vers_minor == 0) { |
||||
parser->http.request.version = GRPC_HTTP_HTTP10; |
||||
} else if (vers_minor == 1) { |
||||
parser->http.request.version = GRPC_HTTP_HTTP11; |
||||
} else { |
||||
goto error; |
||||
} |
||||
} else if (vers_major == 2) { |
||||
if (vers_minor == 0) { |
||||
parser->http.request.version = GRPC_HTTP_HTTP20; |
||||
} else { |
||||
goto error; |
||||
} |
||||
} else { |
||||
goto error; |
||||
} |
||||
|
||||
return 1; |
||||
|
||||
error: |
||||
gpr_log(GPR_ERROR, "Failed parsing request line"); |
||||
return 0; |
||||
} |
||||
|
||||
static int handle_first_line(grpc_http_parser *parser) { |
||||
if (parser->cur_line[0] == 'H') { |
||||
parser->type = GRPC_HTTP_RESPONSE; |
||||
return handle_response_line(parser); |
||||
} else { |
||||
parser->type = GRPC_HTTP_REQUEST; |
||||
return handle_request_line(parser); |
||||
} |
||||
} |
||||
|
||||
static int add_header(grpc_http_parser *parser) { |
||||
uint8_t *beg = parser->cur_line; |
||||
uint8_t *cur = beg; |
||||
uint8_t *end = beg + parser->cur_line_length; |
||||
size_t *hdr_count = NULL; |
||||
grpc_http_header **hdrs = NULL; |
||||
grpc_http_header hdr = {NULL, NULL}; |
||||
|
||||
GPR_ASSERT(cur != end); |
||||
|
||||
if (*cur == ' ' || *cur == '\t') { |
||||
gpr_log(GPR_ERROR, "Continued header lines not supported yet"); |
||||
goto error; |
||||
} |
||||
|
||||
while (cur != end && *cur != ':') { |
||||
cur++; |
||||
} |
||||
if (cur == end) { |
||||
gpr_log(GPR_ERROR, "Didn't find ':' in header string"); |
||||
goto error; |
||||
} |
||||
GPR_ASSERT(cur >= beg); |
||||
hdr.key = buf2str(beg, (size_t)(cur - beg)); |
||||
cur++; /* skip : */ |
||||
|
||||
while (cur != end && (*cur == ' ' || *cur == '\t')) { |
||||
cur++; |
||||
} |
||||
GPR_ASSERT(end - cur >= 2); |
||||
hdr.value = buf2str(cur, (size_t)(end - cur) - 2); |
||||
|
||||
if (parser->type == GRPC_HTTP_RESPONSE) { |
||||
hdr_count = &parser->http.response.hdr_count; |
||||
hdrs = &parser->http.response.hdrs; |
||||
} else if (parser->type == GRPC_HTTP_REQUEST) { |
||||
hdr_count = &parser->http.request.hdr_count; |
||||
hdrs = &parser->http.request.hdrs; |
||||
} else { |
||||
return 0; |
||||
} |
||||
|
||||
if (*hdr_count == parser->hdr_capacity) { |
||||
parser->hdr_capacity = |
||||
GPR_MAX(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2); |
||||
*hdrs = gpr_realloc(*hdrs, parser->hdr_capacity * sizeof(**hdrs)); |
||||
} |
||||
(*hdrs)[(*hdr_count)++] = hdr; |
||||
return 1; |
||||
|
||||
error: |
||||
gpr_free(hdr.key); |
||||
gpr_free(hdr.value); |
||||
return 0; |
||||
} |
||||
|
||||
static int finish_line(grpc_http_parser *parser) { |
||||
switch (parser->state) { |
||||
case GRPC_HTTP_FIRST_LINE: |
||||
if (!handle_first_line(parser)) { |
||||
return 0; |
||||
} |
||||
parser->state = GRPC_HTTP_HEADERS; |
||||
break; |
||||
case GRPC_HTTP_HEADERS: |
||||
if (parser->cur_line_length == 2) { |
||||
parser->state = GRPC_HTTP_BODY; |
||||
break; |
||||
} |
||||
if (!add_header(parser)) { |
||||
return 0; |
||||
} |
||||
break; |
||||
case GRPC_HTTP_BODY: |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
} |
||||
|
||||
parser->cur_line_length = 0; |
||||
return 1; |
||||
} |
||||
|
||||
static int addbyte_body(grpc_http_parser *parser, uint8_t byte) { |
||||
size_t *body_length = NULL; |
||||
char **body = NULL; |
||||
|
||||
if (parser->type == GRPC_HTTP_RESPONSE) { |
||||
body_length = &parser->http.response.body_length; |
||||
body = &parser->http.response.body; |
||||
} else if (parser->type == GRPC_HTTP_REQUEST) { |
||||
body_length = &parser->http.request.body_length; |
||||
body = &parser->http.request.body; |
||||
} else { |
||||
return 0; |
||||
} |
||||
|
||||
if (*body_length == parser->body_capacity) { |
||||
parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2); |
||||
*body = gpr_realloc((void *)*body, parser->body_capacity); |
||||
} |
||||
(*body)[*body_length] = (char)byte; |
||||
(*body_length)++; |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
static int addbyte(grpc_http_parser *parser, uint8_t byte) { |
||||
switch (parser->state) { |
||||
case GRPC_HTTP_FIRST_LINE: |
||||
case GRPC_HTTP_HEADERS: |
||||
if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) { |
||||
gpr_log(GPR_ERROR, "HTTP client max line length (%d) exceeded", |
||||
GRPC_HTTP_PARSER_MAX_HEADER_LENGTH); |
||||
return 0; |
||||
} |
||||
parser->cur_line[parser->cur_line_length] = byte; |
||||
parser->cur_line_length++; |
||||
if (parser->cur_line_length >= 2 && |
||||
parser->cur_line[parser->cur_line_length - 2] == '\r' && |
||||
parser->cur_line[parser->cur_line_length - 1] == '\n') { |
||||
return finish_line(parser); |
||||
} else { |
||||
return 1; |
||||
} |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
case GRPC_HTTP_BODY: |
||||
return addbyte_body(parser, byte); |
||||
} |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
} |
||||
|
||||
void grpc_http_parser_init(grpc_http_parser *parser) { |
||||
memset(parser, 0, sizeof(*parser)); |
||||
parser->state = GRPC_HTTP_FIRST_LINE; |
||||
parser->type = GRPC_HTTP_UNKNOWN; |
||||
} |
||||
|
||||
void grpc_http_parser_destroy(grpc_http_parser *parser) { |
||||
size_t i; |
||||
if (parser->type == GRPC_HTTP_RESPONSE) { |
||||
gpr_free(parser->http.response.body); |
||||
for (i = 0; i < parser->http.response.hdr_count; i++) { |
||||
gpr_free(parser->http.response.hdrs[i].key); |
||||
gpr_free(parser->http.response.hdrs[i].value); |
||||
} |
||||
gpr_free(parser->http.response.hdrs); |
||||
} else if (parser->type == GRPC_HTTP_REQUEST) { |
||||
gpr_free(parser->http.request.body); |
||||
for (i = 0; i < parser->http.request.hdr_count; i++) { |
||||
gpr_free(parser->http.request.hdrs[i].key); |
||||
gpr_free(parser->http.request.hdrs[i].value); |
||||
} |
||||
gpr_free(parser->http.request.hdrs); |
||||
gpr_free(parser->http.request.method); |
||||
gpr_free(parser->http.request.path); |
||||
} |
||||
} |
||||
|
||||
int grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice) { |
||||
size_t i; |
||||
|
||||
for (i = 0; i < GPR_SLICE_LENGTH(slice); i++) { |
||||
if (!addbyte(parser, GPR_SLICE_START_PTR(slice)[i])) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
int grpc_http_parser_eof(grpc_http_parser *parser) { |
||||
return parser->state == GRPC_HTTP_BODY; |
||||
} |
@ -0,0 +1,116 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_HTTP_PARSER_H |
||||
#define GRPC_CORE_HTTP_PARSER_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#include <grpc/support/slice.h> |
||||
|
||||
/* Maximum length of a header string of the form 'Key: Value\r\n' */ |
||||
#define GRPC_HTTP_PARSER_MAX_HEADER_LENGTH 4096 |
||||
|
||||
/* A single header to be passed in a request */ |
||||
typedef struct grpc_http_header { |
||||
char *key; |
||||
char *value; |
||||
} grpc_http_header; |
||||
|
||||
typedef enum { |
||||
GRPC_HTTP_FIRST_LINE, |
||||
GRPC_HTTP_HEADERS, |
||||
GRPC_HTTP_BODY |
||||
} grpc_http_parser_state; |
||||
|
||||
typedef enum { |
||||
GRPC_HTTP_HTTP10, |
||||
GRPC_HTTP_HTTP11, |
||||
GRPC_HTTP_HTTP20, |
||||
} grpc_http_version; |
||||
|
||||
typedef enum { |
||||
GRPC_HTTP_RESPONSE, |
||||
GRPC_HTTP_REQUEST, |
||||
GRPC_HTTP_UNKNOWN |
||||
} grpc_http_type; |
||||
|
||||
/* A request */ |
||||
typedef struct grpc_http_request { |
||||
/* Method of the request (e.g. GET, POST) */ |
||||
char *method; |
||||
/* The path of the resource to fetch */ |
||||
char *path; |
||||
/* HTTP version to use */ |
||||
grpc_http_version version; |
||||
/* Headers attached to the request */ |
||||
size_t hdr_count; |
||||
grpc_http_header *hdrs; |
||||
/* Body: length and contents; contents are NOT null-terminated */ |
||||
size_t body_length; |
||||
char *body; |
||||
} grpc_http_request; |
||||
|
||||
/* A response */ |
||||
typedef struct grpc_http_response { |
||||
/* HTTP status code */ |
||||
int status; |
||||
/* Headers: count and key/values */ |
||||
size_t hdr_count; |
||||
grpc_http_header *hdrs; |
||||
/* Body: length and contents; contents are NOT null-terminated */ |
||||
size_t body_length; |
||||
char *body; |
||||
} grpc_http_response; |
||||
|
||||
typedef struct { |
||||
grpc_http_parser_state state; |
||||
grpc_http_type type; |
||||
|
||||
union { |
||||
grpc_http_response response; |
||||
grpc_http_request request; |
||||
} http; |
||||
size_t body_capacity; |
||||
size_t hdr_capacity; |
||||
|
||||
uint8_t cur_line[GRPC_HTTP_PARSER_MAX_HEADER_LENGTH]; |
||||
size_t cur_line_length; |
||||
} grpc_http_parser; |
||||
|
||||
void grpc_http_parser_init(grpc_http_parser *parser); |
||||
void grpc_http_parser_destroy(grpc_http_parser *parser); |
||||
|
||||
int grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice); |
||||
int grpc_http_parser_eof(grpc_http_parser *parser); |
||||
|
||||
#endif /* GRPC_CORE_HTTP_PARSER_H */ |
@ -1,219 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/httpcli/parser.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/useful.h> |
||||
|
||||
extern int grpc_http_trace; |
||||
|
||||
static int handle_response_line(grpc_httpcli_parser *parser) { |
||||
uint8_t *beg = parser->cur_line; |
||||
uint8_t *cur = beg; |
||||
uint8_t *end = beg + parser->cur_line_length; |
||||
|
||||
if (cur == end || *cur++ != 'H') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'T') goto error; |
||||
if (cur == end || *cur++ != 'P') goto error; |
||||
if (cur == end || *cur++ != '/') goto error; |
||||
if (cur == end || *cur++ != '1') goto error; |
||||
if (cur == end || *cur++ != '.') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '1') goto error; |
||||
if (cur == end || *cur++ != ' ') goto error; |
||||
if (cur == end || *cur < '1' || *cur++ > '9') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
||||
if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
||||
parser->r.status = |
||||
(cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0'); |
||||
if (cur == end || *cur++ != ' ') goto error; |
||||
|
||||
/* we don't really care about the status code message */ |
||||
|
||||
return 1; |
||||
|
||||
error: |
||||
if (grpc_http_trace) { |
||||
gpr_log(GPR_ERROR, "Failed parsing response line"); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
static char *buf2str(void *buffer, size_t length) { |
||||
char *out = gpr_malloc(length + 1); |
||||
memcpy(out, buffer, length); |
||||
out[length] = 0; |
||||
return out; |
||||
} |
||||
|
||||
static int add_header(grpc_httpcli_parser *parser) { |
||||
uint8_t *beg = parser->cur_line; |
||||
uint8_t *cur = beg; |
||||
uint8_t *end = beg + parser->cur_line_length; |
||||
grpc_httpcli_header hdr = {NULL, NULL}; |
||||
|
||||
GPR_ASSERT(cur != end); |
||||
|
||||
if (*cur == ' ' || *cur == '\t') { |
||||
if (grpc_http_trace) { |
||||
gpr_log(GPR_ERROR, "Continued header lines not supported yet"); |
||||
} |
||||
goto error; |
||||
} |
||||
|
||||
while (cur != end && *cur != ':') { |
||||
cur++; |
||||
} |
||||
if (cur == end) { |
||||
if (grpc_http_trace) { |
||||
gpr_log(GPR_ERROR, "Didn't find ':' in header string"); |
||||
} |
||||
goto error; |
||||
} |
||||
GPR_ASSERT(cur >= beg); |
||||
hdr.key = buf2str(beg, (size_t)(cur - beg)); |
||||
cur++; /* skip : */ |
||||
|
||||
while (cur != end && (*cur == ' ' || *cur == '\t')) { |
||||
cur++; |
||||
} |
||||
GPR_ASSERT(end - cur >= 2); |
||||
hdr.value = buf2str(cur, (size_t)(end - cur) - 2); |
||||
|
||||
if (parser->r.hdr_count == parser->hdr_capacity) { |
||||
parser->hdr_capacity = |
||||
GPR_MAX(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2); |
||||
parser->r.hdrs = gpr_realloc( |
||||
parser->r.hdrs, parser->hdr_capacity * sizeof(*parser->r.hdrs)); |
||||
} |
||||
parser->r.hdrs[parser->r.hdr_count++] = hdr; |
||||
return 1; |
||||
|
||||
error: |
||||
gpr_free(hdr.key); |
||||
gpr_free(hdr.value); |
||||
return 0; |
||||
} |
||||
|
||||
static int finish_line(grpc_httpcli_parser *parser) { |
||||
switch (parser->state) { |
||||
case GRPC_HTTPCLI_INITIAL_RESPONSE: |
||||
if (!handle_response_line(parser)) { |
||||
return 0; |
||||
} |
||||
parser->state = GRPC_HTTPCLI_HEADERS; |
||||
break; |
||||
case GRPC_HTTPCLI_HEADERS: |
||||
if (parser->cur_line_length == 2) { |
||||
parser->state = GRPC_HTTPCLI_BODY; |
||||
break; |
||||
} |
||||
if (!add_header(parser)) { |
||||
return 0; |
||||
} |
||||
break; |
||||
case GRPC_HTTPCLI_BODY: |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
} |
||||
|
||||
parser->cur_line_length = 0; |
||||
return 1; |
||||
} |
||||
|
||||
static int addbyte(grpc_httpcli_parser *parser, uint8_t byte) { |
||||
switch (parser->state) { |
||||
case GRPC_HTTPCLI_INITIAL_RESPONSE: |
||||
case GRPC_HTTPCLI_HEADERS: |
||||
if (parser->cur_line_length >= GRPC_HTTPCLI_MAX_HEADER_LENGTH) { |
||||
gpr_log(GPR_ERROR, "HTTP client max line length (%d) exceeded", |
||||
GRPC_HTTPCLI_MAX_HEADER_LENGTH); |
||||
return 0; |
||||
} |
||||
parser->cur_line[parser->cur_line_length] = byte; |
||||
parser->cur_line_length++; |
||||
if (parser->cur_line_length >= 2 && |
||||
parser->cur_line[parser->cur_line_length - 2] == '\r' && |
||||
parser->cur_line[parser->cur_line_length - 1] == '\n') { |
||||
return finish_line(parser); |
||||
} else { |
||||
return 1; |
||||
} |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
case GRPC_HTTPCLI_BODY: |
||||
if (parser->r.body_length == parser->body_capacity) { |
||||
parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2); |
||||
parser->r.body = |
||||
gpr_realloc((void *)parser->r.body, parser->body_capacity); |
||||
} |
||||
parser->r.body[parser->r.body_length] = (char)byte; |
||||
parser->r.body_length++; |
||||
return 1; |
||||
} |
||||
GPR_UNREACHABLE_CODE(return 0); |
||||
} |
||||
|
||||
void grpc_httpcli_parser_init(grpc_httpcli_parser *parser) { |
||||
memset(parser, 0, sizeof(*parser)); |
||||
parser->state = GRPC_HTTPCLI_INITIAL_RESPONSE; |
||||
parser->r.status = 500; |
||||
} |
||||
|
||||
void grpc_httpcli_parser_destroy(grpc_httpcli_parser *parser) { |
||||
size_t i; |
||||
gpr_free(parser->r.body); |
||||
for (i = 0; i < parser->r.hdr_count; i++) { |
||||
gpr_free(parser->r.hdrs[i].key); |
||||
gpr_free(parser->r.hdrs[i].value); |
||||
} |
||||
gpr_free(parser->r.hdrs); |
||||
} |
||||
|
||||
int grpc_httpcli_parser_parse(grpc_httpcli_parser *parser, gpr_slice slice) { |
||||
size_t i; |
||||
|
||||
for (i = 0; i < GPR_SLICE_LENGTH(slice); i++) { |
||||
if (!addbyte(parser, GPR_SLICE_START_PTR(slice)[i])) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
int grpc_httpcli_parser_eof(grpc_httpcli_parser *parser) { |
||||
return parser->state == GRPC_HTTPCLI_BODY; |
||||
} |
@ -0,0 +1,103 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/iomgr/unix_sockets_posix.h" |
||||
|
||||
#ifdef GPR_HAVE_UNIX_SOCKET |
||||
|
||||
#include <string.h> |
||||
#include <sys/types.h> |
||||
#include <sys/stat.h> |
||||
#include <sys/un.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
|
||||
void grpc_create_socketpair_if_unix(int sv[2]) { |
||||
GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
||||
} |
||||
|
||||
grpc_resolved_addresses *grpc_resolve_unix_domain_address(const char *name) { |
||||
struct sockaddr_un *un; |
||||
|
||||
grpc_resolved_addresses *addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
||||
addrs->naddrs = 1; |
||||
addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address)); |
||||
un = (struct sockaddr_un *)addrs->addrs->addr; |
||||
un->sun_family = AF_UNIX; |
||||
strcpy(un->sun_path, name); |
||||
addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
||||
return addrs; |
||||
} |
||||
|
||||
int grpc_is_unix_socket(const struct sockaddr *addr) { |
||||
return addr->sa_family == AF_UNIX; |
||||
} |
||||
|
||||
void grpc_unlink_if_unix_domain_socket(const struct sockaddr *addr) { |
||||
if (addr->sa_family != AF_UNIX) { |
||||
return; |
||||
} |
||||
struct sockaddr_un *un = (struct sockaddr_un *)addr; |
||||
struct stat st; |
||||
|
||||
if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
||||
unlink(un->sun_path); |
||||
} |
||||
} |
||||
|
||||
int grpc_parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) { |
||||
struct sockaddr_un *un = (struct sockaddr_un *)addr; |
||||
|
||||
un->sun_family = AF_UNIX; |
||||
strcpy(un->sun_path, uri->path); |
||||
*len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
char *grpc_unix_get_default_authority(grpc_resolver_factory *factory, |
||||
grpc_uri *uri) { |
||||
return gpr_strdup("localhost"); |
||||
} |
||||
|
||||
char *grpc_sockaddr_to_uri_unix_if_possible(const struct sockaddr *addr) { |
||||
if (addr->sa_family != AF_UNIX) { |
||||
return NULL; |
||||
} |
||||
|
||||
char *result; |
||||
gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); |
||||
return result; |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,61 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/iomgr/unix_sockets_posix.h" |
||||
|
||||
#ifndef GPR_HAVE_UNIX_SOCKET |
||||
|
||||
void grpc_create_socketpair_if_unix(int sv[2]) {} |
||||
|
||||
grpc_resolved_addresses *grpc_resolve_unix_domain_address(const char *name) { |
||||
return NULL; |
||||
} |
||||
|
||||
int grpc_is_unix_socket(const struct sockaddr *addr) { return false; } |
||||
|
||||
void grpc_unlink_if_unix_domain_socket(const struct sockaddr *addr) {} |
||||
|
||||
int grpc_parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) { |
||||
return 0; |
||||
} |
||||
|
||||
char *grpc_unix_get_default_authority(grpc_resolver_factory *factory, |
||||
grpc_uri *uri) { |
||||
return NULL; |
||||
} |
||||
|
||||
char *grpc_sockaddr_to_uri_unix_if_possible(const struct sockaddr *addr) { |
||||
return NULL; |
||||
} |
||||
|
||||
#endif |
@ -1,350 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2016, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/host_port.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/sync.h> |
||||
#include <grpc/support/thd.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "src/core/channel/channel_args.h" |
||||
#include "src/core/channel/client_channel.h" |
||||
#include "src/core/channel/client_uchannel.h" |
||||
#include "src/core/channel/connected_channel.h" |
||||
#include "src/core/channel/http_client_filter.h" |
||||
#include "src/core/channel/http_server_filter.h" |
||||
#include "src/core/client_config/resolver_registry.h" |
||||
#include "src/core/iomgr/tcp_client.h" |
||||
#include "src/core/surface/channel.h" |
||||
#include "src/core/surface/server.h" |
||||
#include "src/core/transport/chttp2_transport.h" |
||||
#include "test/core/util/port.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
typedef struct { |
||||
grpc_connector base; |
||||
gpr_refcount refs; |
||||
|
||||
grpc_closure *notify; |
||||
grpc_connect_in_args args; |
||||
grpc_connect_out_args *result; |
||||
|
||||
grpc_endpoint *tcp; |
||||
|
||||
grpc_closure connected; |
||||
} connector; |
||||
|
||||
static void connector_ref(grpc_connector *con) { |
||||
connector *c = (connector *)con; |
||||
gpr_ref(&c->refs); |
||||
} |
||||
|
||||
static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) { |
||||
connector *c = (connector *)con; |
||||
if (gpr_unref(&c->refs)) { |
||||
gpr_free(c); |
||||
} |
||||
} |
||||
|
||||
static void connected(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
||||
connector *c = arg; |
||||
grpc_closure *notify; |
||||
grpc_endpoint *tcp = c->tcp; |
||||
if (tcp != NULL) { |
||||
c->result->transport = |
||||
grpc_create_chttp2_transport(exec_ctx, c->args.channel_args, tcp, 1); |
||||
grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL, |
||||
0); |
||||
GPR_ASSERT(c->result->transport); |
||||
} else { |
||||
memset(c->result, 0, sizeof(*c->result)); |
||||
} |
||||
notify = c->notify; |
||||
c->notify = NULL; |
||||
notify->cb(exec_ctx, notify->cb_arg, 1); |
||||
} |
||||
|
||||
static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {} |
||||
|
||||
static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con, |
||||
const grpc_connect_in_args *args, |
||||
grpc_connect_out_args *result, |
||||
grpc_closure *notify) { |
||||
connector *c = (connector *)con; |
||||
GPR_ASSERT(c->notify == NULL); |
||||
GPR_ASSERT(notify->cb); |
||||
c->notify = notify; |
||||
c->args = *args; |
||||
c->result = result; |
||||
c->tcp = NULL; |
||||
grpc_closure_init(&c->connected, connected, c); |
||||
grpc_tcp_client_connect(exec_ctx, &c->connected, &c->tcp, |
||||
args->interested_parties, args->addr, args->addr_len, |
||||
args->deadline); |
||||
} |
||||
|
||||
static const grpc_connector_vtable connector_vtable = { |
||||
connector_ref, connector_unref, connector_shutdown, connector_connect}; |
||||
|
||||
typedef struct { |
||||
grpc_subchannel_factory base; |
||||
gpr_refcount refs; |
||||
grpc_channel_args *merge_args; |
||||
grpc_channel *master; |
||||
grpc_subchannel **sniffed_subchannel; |
||||
} subchannel_factory; |
||||
|
||||
static void subchannel_factory_ref(grpc_subchannel_factory *scf) { |
||||
subchannel_factory *f = (subchannel_factory *)scf; |
||||
gpr_ref(&f->refs); |
||||
} |
||||
|
||||
static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx, |
||||
grpc_subchannel_factory *scf) { |
||||
subchannel_factory *f = (subchannel_factory *)scf; |
||||
if (gpr_unref(&f->refs)) { |
||||
GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, "subchannel_factory"); |
||||
grpc_channel_args_destroy(f->merge_args); |
||||
gpr_free(f); |
||||
} |
||||
} |
||||
|
||||
static grpc_subchannel *subchannel_factory_create_subchannel( |
||||
grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf, |
||||
grpc_subchannel_args *args) { |
||||
subchannel_factory *f = (subchannel_factory *)scf; |
||||
connector *c = gpr_malloc(sizeof(*c)); |
||||
grpc_channel_args *final_args = |
||||
grpc_channel_args_merge(args->args, f->merge_args); |
||||
grpc_subchannel *s; |
||||
memset(c, 0, sizeof(*c)); |
||||
c->base.vtable = &connector_vtable; |
||||
gpr_ref_init(&c->refs, 1); |
||||
args->args = final_args; |
||||
s = grpc_subchannel_create(exec_ctx, &c->base, args); |
||||
grpc_connector_unref(exec_ctx, &c->base); |
||||
grpc_channel_args_destroy(final_args); |
||||
if (*f->sniffed_subchannel) { |
||||
GRPC_SUBCHANNEL_UNREF(exec_ctx, *f->sniffed_subchannel, "sniffed"); |
||||
} |
||||
*f->sniffed_subchannel = s; |
||||
GRPC_SUBCHANNEL_REF(s, "sniffed"); |
||||
return s; |
||||
} |
||||
|
||||
static const grpc_subchannel_factory_vtable test_subchannel_factory_vtable = { |
||||
subchannel_factory_ref, subchannel_factory_unref, |
||||
subchannel_factory_create_subchannel}; |
||||
|
||||
/* The evil twin of grpc_insecure_channel_create. It allows the test to use the
|
||||
* custom-built sniffing subchannel_factory */ |
||||
grpc_channel *channel_create(const char *target, const grpc_channel_args *args, |
||||
grpc_subchannel **sniffed_subchannel) { |
||||
grpc_channel *channel = NULL; |
||||
grpc_resolver *resolver; |
||||
subchannel_factory *f; |
||||
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
|
||||
channel = |
||||
grpc_channel_create(&exec_ctx, target, args, GRPC_CLIENT_CHANNEL, NULL); |
||||
|
||||
f = gpr_malloc(sizeof(*f)); |
||||
f->sniffed_subchannel = sniffed_subchannel; |
||||
f->base.vtable = &test_subchannel_factory_vtable; |
||||
gpr_ref_init(&f->refs, 1); |
||||
f->merge_args = grpc_channel_args_copy(args); |
||||
f->master = channel; |
||||
GRPC_CHANNEL_INTERNAL_REF(f->master, "test_subchannel_factory"); |
||||
resolver = grpc_resolver_create(target, &f->base); |
||||
if (!resolver) { |
||||
return NULL; |
||||
} |
||||
|
||||
grpc_client_channel_set_resolver( |
||||
&exec_ctx, grpc_channel_get_channel_stack(channel), resolver); |
||||
GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_create"); |
||||
grpc_subchannel_factory_unref(&exec_ctx, &f->base); |
||||
|
||||
grpc_exec_ctx_finish(&exec_ctx); |
||||
|
||||
return channel; |
||||
} |
||||
|
||||
typedef struct micro_fullstack_fixture_data { |
||||
char *localaddr; |
||||
grpc_channel *master_channel; |
||||
grpc_subchannel *sniffed_subchannel; |
||||
} micro_fullstack_fixture_data; |
||||
|
||||
static grpc_end2end_test_fixture chttp2_create_fixture_micro_fullstack( |
||||
grpc_channel_args *client_args, grpc_channel_args *server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
int port = grpc_pick_unused_port_or_die(); |
||||
micro_fullstack_fixture_data *ffd = |
||||
gpr_malloc(sizeof(micro_fullstack_fixture_data)); |
||||
memset(&f, 0, sizeof(f)); |
||||
memset(ffd, 0, sizeof(*ffd)); |
||||
|
||||
gpr_join_host_port(&ffd->localaddr, "127.0.0.1", port); |
||||
|
||||
f.fixture_data = ffd; |
||||
f.cq = grpc_completion_queue_create(NULL); |
||||
|
||||
return f; |
||||
} |
||||
|
||||
grpc_connectivity_state g_state = GRPC_CHANNEL_IDLE; |
||||
grpc_pollset_set *g_interested_parties; |
||||
|
||||
static void state_changed(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
||||
if (g_state != GRPC_CHANNEL_READY) { |
||||
grpc_subchannel_notify_on_state_change( |
||||
exec_ctx, arg, g_interested_parties, &g_state, |
||||
grpc_closure_create(state_changed, arg)); |
||||
} |
||||
} |
||||
|
||||
static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
||||
grpc_pollset_destroy(arg); |
||||
} |
||||
|
||||
static grpc_connected_subchannel *connect_subchannel(grpc_subchannel *c) { |
||||
gpr_mu *mu; |
||||
grpc_pollset *pollset = gpr_malloc(grpc_pollset_size()); |
||||
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
grpc_pollset_init(pollset, &mu); |
||||
g_interested_parties = grpc_pollset_set_create(); |
||||
grpc_pollset_set_add_pollset(&exec_ctx, g_interested_parties, pollset); |
||||
grpc_subchannel_notify_on_state_change(&exec_ctx, c, g_interested_parties, |
||||
&g_state, |
||||
grpc_closure_create(state_changed, c)); |
||||
grpc_exec_ctx_flush(&exec_ctx); |
||||
gpr_mu_lock(mu); |
||||
while (g_state != GRPC_CHANNEL_READY) { |
||||
grpc_pollset_worker *worker = NULL; |
||||
grpc_pollset_work(&exec_ctx, pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), |
||||
GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1)); |
||||
gpr_mu_unlock(mu); |
||||
grpc_exec_ctx_flush(&exec_ctx); |
||||
gpr_mu_lock(mu); |
||||
} |
||||
grpc_pollset_shutdown(&exec_ctx, pollset, |
||||
grpc_closure_create(destroy_pollset, pollset)); |
||||
grpc_pollset_set_destroy(g_interested_parties); |
||||
gpr_mu_unlock(mu); |
||||
grpc_exec_ctx_finish(&exec_ctx); |
||||
gpr_free(pollset); |
||||
return grpc_subchannel_get_connected_subchannel(c); |
||||
} |
||||
|
||||
static void chttp2_init_client_micro_fullstack(grpc_end2end_test_fixture *f, |
||||
grpc_channel_args *client_args) { |
||||
micro_fullstack_fixture_data *ffd = f->fixture_data; |
||||
grpc_connectivity_state conn_state; |
||||
grpc_connected_subchannel *connected_subchannel; |
||||
char *ipv4_localaddr; |
||||
|
||||
gpr_asprintf(&ipv4_localaddr, "ipv4:%s", ffd->localaddr); |
||||
ffd->master_channel = |
||||
channel_create(ipv4_localaddr, client_args, &ffd->sniffed_subchannel); |
||||
gpr_free(ipv4_localaddr); |
||||
gpr_log(GPR_INFO, "MASTER CHANNEL %p ", ffd->master_channel); |
||||
/* the following will block. That's ok for this test */ |
||||
conn_state = grpc_channel_check_connectivity_state(ffd->master_channel, |
||||
1 /* try to connect */); |
||||
GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE); |
||||
|
||||
/* here sniffed_subchannel should be ready to use */ |
||||
GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE); |
||||
GPR_ASSERT(ffd->sniffed_subchannel != NULL); |
||||
|
||||
connected_subchannel = connect_subchannel(ffd->sniffed_subchannel); |
||||
f->client = grpc_client_uchannel_create(ffd->sniffed_subchannel, client_args); |
||||
grpc_client_uchannel_set_connected_subchannel(f->client, |
||||
connected_subchannel); |
||||
gpr_log(GPR_INFO, "CHANNEL WRAPPING SUBCHANNEL: %p(%p)", f->client, |
||||
ffd->sniffed_subchannel); |
||||
|
||||
GPR_ASSERT(f->client); |
||||
} |
||||
|
||||
static void chttp2_init_server_micro_fullstack(grpc_end2end_test_fixture *f, |
||||
grpc_channel_args *server_args) { |
||||
micro_fullstack_fixture_data *ffd = f->fixture_data; |
||||
if (f->server) { |
||||
grpc_server_destroy(f->server); |
||||
} |
||||
f->server = grpc_server_create(server_args, NULL); |
||||
grpc_server_register_completion_queue(f->server, f->cq, NULL); |
||||
GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); |
||||
grpc_server_start(f->server); |
||||
} |
||||
|
||||
static void chttp2_tear_down_micro_fullstack(grpc_end2end_test_fixture *f) { |
||||
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
micro_fullstack_fixture_data *ffd = f->fixture_data; |
||||
grpc_channel_destroy(ffd->master_channel); |
||||
if (ffd->sniffed_subchannel) { |
||||
GRPC_SUBCHANNEL_UNREF(&exec_ctx, ffd->sniffed_subchannel, "sniffed"); |
||||
} |
||||
gpr_free(ffd->localaddr); |
||||
gpr_free(ffd); |
||||
grpc_exec_ctx_finish(&exec_ctx); |
||||
} |
||||
|
||||
/* All test configurations */ |
||||
static grpc_end2end_test_config configs[] = { |
||||
{"chttp2/micro_fullstack", 0, chttp2_create_fixture_micro_fullstack, |
||||
chttp2_init_client_micro_fullstack, chttp2_init_server_micro_fullstack, |
||||
chttp2_tear_down_micro_fullstack}, |
||||
}; |
||||
|
||||
int main(int argc, char **argv) { |
||||
size_t i; |
||||
|
||||
grpc_test_init(argc, argv); |
||||
grpc_init(); |
||||
|
||||
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
||||
grpc_end2end_tests(argc, argv, configs[i]); |
||||
} |
||||
|
||||
grpc_shutdown(); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,3 @@ |
||||
GET / HTTP/1.0 |
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue