LCOV - code coverage report
Current view: top level - core/httpcli - httpcli.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 120 126 95.2 %
Date: 2015-12-10 22:15:08 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       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             : 
      34             : #include "src/core/iomgr/sockaddr.h"
      35             : #include "src/core/httpcli/httpcli.h"
      36             : 
      37             : #include <string.h>
      38             : 
      39             : #include "src/core/iomgr/endpoint.h"
      40             : #include "src/core/iomgr/resolve_address.h"
      41             : #include "src/core/iomgr/tcp_client.h"
      42             : #include "src/core/httpcli/format_request.h"
      43             : #include "src/core/httpcli/parser.h"
      44             : #include "src/core/support/string.h"
      45             : #include <grpc/support/alloc.h>
      46             : #include <grpc/support/log.h>
      47             : #include <grpc/support/string_util.h>
      48             : 
      49             : typedef struct {
      50             :   gpr_slice request_text;
      51             :   grpc_httpcli_parser parser;
      52             :   grpc_resolved_addresses *addresses;
      53             :   size_t next_address;
      54             :   grpc_endpoint *ep;
      55             :   char *host;
      56             :   char *ssl_host_override;
      57             :   gpr_timespec deadline;
      58             :   int have_read_byte;
      59             :   const grpc_httpcli_handshaker *handshaker;
      60             :   grpc_httpcli_response_cb on_response;
      61             :   void *user_data;
      62             :   grpc_httpcli_context *context;
      63             :   grpc_pollset *pollset;
      64             :   grpc_iomgr_object iomgr_obj;
      65             :   gpr_slice_buffer incoming;
      66             :   gpr_slice_buffer outgoing;
      67             :   grpc_closure on_read;
      68             :   grpc_closure done_write;
      69             :   grpc_closure connected;
      70             : } internal_request;
      71             : 
      72             : static grpc_httpcli_get_override g_get_override = NULL;
      73             : static grpc_httpcli_post_override g_post_override = NULL;
      74             : 
      75        4338 : static void plaintext_handshake(grpc_exec_ctx *exec_ctx, void *arg,
      76             :                                 grpc_endpoint *endpoint, const char *host,
      77             :                                 void (*on_done)(grpc_exec_ctx *exec_ctx,
      78             :                                                 void *arg,
      79             :                                                 grpc_endpoint *endpoint)) {
      80        4338 :   on_done(exec_ctx, arg, endpoint);
      81        4338 : }
      82             : 
      83             : const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http",
      84             :                                                         plaintext_handshake};
      85             : 
      86        4352 : void grpc_httpcli_context_init(grpc_httpcli_context *context) {
      87        4352 :   grpc_pollset_set_init(&context->pollset_set);
      88        4352 : }
      89             : 
      90        4351 : void grpc_httpcli_context_destroy(grpc_httpcli_context *context) {
      91        4351 :   grpc_pollset_set_destroy(&context->pollset_set);
      92        4351 : }
      93             : 
      94             : static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req);
      95             : 
      96        4340 : static void finish(grpc_exec_ctx *exec_ctx, internal_request *req,
      97             :                    int success) {
      98        4340 :   grpc_pollset_set_del_pollset(exec_ctx, &req->context->pollset_set,
      99             :                                req->pollset);
     100        4340 :   req->on_response(exec_ctx, req->user_data, success ? &req->parser.r : NULL);
     101        4340 :   grpc_httpcli_parser_destroy(&req->parser);
     102        4340 :   if (req->addresses != NULL) {
     103        4340 :     grpc_resolved_addresses_destroy(req->addresses);
     104             :   }
     105        4340 :   if (req->ep != NULL) {
     106        4340 :     grpc_endpoint_destroy(exec_ctx, req->ep);
     107             :   }
     108        4340 :   gpr_slice_unref(req->request_text);
     109        4340 :   gpr_free(req->host);
     110        4340 :   gpr_free(req->ssl_host_override);
     111        4340 :   grpc_iomgr_unregister_object(&req->iomgr_obj);
     112        4340 :   gpr_slice_buffer_destroy(&req->incoming);
     113        4340 :   gpr_slice_buffer_destroy(&req->outgoing);
     114        4340 :   gpr_free(req);
     115        4340 : }
     116             : 
     117             : static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success);
     118             : 
     119       16098 : static void do_read(grpc_exec_ctx *exec_ctx, internal_request *req) {
     120       16098 :   grpc_endpoint_read(exec_ctx, req->ep, &req->incoming, &req->on_read);
     121       16098 : }
     122             : 
     123       16098 : static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
     124       16098 :   internal_request *req = user_data;
     125             :   size_t i;
     126             : 
     127       27856 :   for (i = 0; i < req->incoming.count; i++) {
     128       11758 :     if (GPR_SLICE_LENGTH(req->incoming.slices[i])) {
     129       11758 :       req->have_read_byte = 1;
     130       11758 :       if (!grpc_httpcli_parser_parse(&req->parser, req->incoming.slices[i])) {
     131           0 :         finish(exec_ctx, req, 0);
     132       16098 :         return;
     133             :       }
     134             :     }
     135             :   }
     136             : 
     137       16098 :   if (success) {
     138       11758 :     do_read(exec_ctx, req);
     139        4340 :   } else if (!req->have_read_byte) {
     140           0 :     next_address(exec_ctx, req);
     141             :   } else {
     142        4340 :     finish(exec_ctx, req, grpc_httpcli_parser_eof(&req->parser));
     143             :   }
     144             : }
     145             : 
     146        4340 : static void on_written(grpc_exec_ctx *exec_ctx, internal_request *req) {
     147        4340 :   do_read(exec_ctx, req);
     148        4340 : }
     149             : 
     150        4340 : static void done_write(grpc_exec_ctx *exec_ctx, void *arg, int success) {
     151        4340 :   internal_request *req = arg;
     152        4340 :   if (success) {
     153        4340 :     on_written(exec_ctx, req);
     154             :   } else {
     155           0 :     next_address(exec_ctx, req);
     156             :   }
     157        4340 : }
     158             : 
     159        4340 : static void start_write(grpc_exec_ctx *exec_ctx, internal_request *req) {
     160        4340 :   gpr_slice_ref(req->request_text);
     161        4340 :   gpr_slice_buffer_add(&req->outgoing, req->request_text);
     162        4340 :   grpc_endpoint_write(exec_ctx, req->ep, &req->outgoing, &req->done_write);
     163        4340 : }
     164             : 
     165        4340 : static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
     166             :                               grpc_endpoint *ep) {
     167        4340 :   internal_request *req = arg;
     168             : 
     169        4340 :   if (!ep) {
     170           0 :     next_address(exec_ctx, req);
     171        4340 :     return;
     172             :   }
     173             : 
     174        4340 :   req->ep = ep;
     175        4340 :   start_write(exec_ctx, req);
     176             : }
     177             : 
     178        8680 : static void on_connected(grpc_exec_ctx *exec_ctx, void *arg, int success) {
     179        8680 :   internal_request *req = arg;
     180             : 
     181        8680 :   if (!req->ep) {
     182        4340 :     next_address(exec_ctx, req);
     183       13020 :     return;
     184             :   }
     185        8680 :   req->handshaker->handshake(
     186             :       exec_ctx, req, req->ep,
     187        4340 :       req->ssl_host_override ? req->ssl_host_override : req->host,
     188             :       on_handshake_done);
     189             : }
     190             : 
     191        8680 : static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req) {
     192             :   grpc_resolved_address *addr;
     193        8680 :   if (req->next_address == req->addresses->naddrs) {
     194           0 :     finish(exec_ctx, req, 0);
     195        8680 :     return;
     196             :   }
     197        8680 :   addr = &req->addresses->addrs[req->next_address++];
     198        8680 :   grpc_closure_init(&req->connected, on_connected, req);
     199       17360 :   grpc_tcp_client_connect(
     200        8680 :       exec_ctx, &req->connected, &req->ep, &req->context->pollset_set,
     201        8680 :       (struct sockaddr *)&addr->addr, addr->len, req->deadline);
     202             : }
     203             : 
     204        4340 : static void on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
     205             :                         grpc_resolved_addresses *addresses) {
     206        4340 :   internal_request *req = arg;
     207        4340 :   if (!addresses) {
     208           0 :     finish(exec_ctx, req, 0);
     209        4340 :     return;
     210             :   }
     211        4340 :   req->addresses = addresses;
     212        4340 :   req->next_address = 0;
     213        4340 :   next_address(exec_ctx, req);
     214             : }
     215             : 
     216        4340 : static void internal_request_begin(
     217             :     grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     218             :     grpc_pollset *pollset, const grpc_httpcli_request *request,
     219             :     gpr_timespec deadline, grpc_httpcli_response_cb on_response,
     220             :     void *user_data, const char *name, gpr_slice request_text) {
     221        4340 :   internal_request *req = gpr_malloc(sizeof(internal_request));
     222        4340 :   memset(req, 0, sizeof(*req));
     223        4340 :   req->request_text = request_text;
     224        4340 :   grpc_httpcli_parser_init(&req->parser);
     225        4340 :   req->on_response = on_response;
     226        4340 :   req->user_data = user_data;
     227        4340 :   req->deadline = deadline;
     228        4340 :   req->handshaker =
     229        4340 :       request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
     230        4340 :   req->context = context;
     231        4340 :   req->pollset = pollset;
     232        4340 :   grpc_closure_init(&req->on_read, on_read, req);
     233        4340 :   grpc_closure_init(&req->done_write, done_write, req);
     234        4340 :   gpr_slice_buffer_init(&req->incoming);
     235        4340 :   gpr_slice_buffer_init(&req->outgoing);
     236        4340 :   grpc_iomgr_register_object(&req->iomgr_obj, name);
     237        4340 :   req->host = gpr_strdup(request->host);
     238        4340 :   req->ssl_host_override = gpr_strdup(request->ssl_host_override);
     239             : 
     240        4340 :   grpc_pollset_set_add_pollset(exec_ctx, &req->context->pollset_set,
     241             :                                req->pollset);
     242        4340 :   grpc_resolve_address(request->host, req->handshaker->default_port,
     243             :                        on_resolved, req);
     244        4340 : }
     245             : 
     246        4350 : void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     247             :                       grpc_pollset *pollset,
     248             :                       const grpc_httpcli_request *request,
     249             :                       gpr_timespec deadline,
     250             :                       grpc_httpcli_response_cb on_response, void *user_data) {
     251             :   char *name;
     252        4362 :   if (g_get_override &&
     253          12 :       g_get_override(exec_ctx, request, deadline, on_response, user_data)) {
     254        4362 :     return;
     255             :   }
     256        4338 :   gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path);
     257        4338 :   internal_request_begin(exec_ctx, context, pollset, request, deadline,
     258             :                          on_response, user_data, name,
     259             :                          grpc_httpcli_format_get_request(request));
     260        4338 :   gpr_free(name);
     261             : }
     262             : 
     263           4 : void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     264             :                        grpc_pollset *pollset,
     265             :                        const grpc_httpcli_request *request,
     266             :                        const char *body_bytes, size_t body_size,
     267             :                        gpr_timespec deadline,
     268             :                        grpc_httpcli_response_cb on_response, void *user_data) {
     269             :   char *name;
     270           6 :   if (g_post_override &&
     271           2 :       g_post_override(exec_ctx, request, body_bytes, body_size, deadline,
     272             :                       on_response, user_data)) {
     273           6 :     return;
     274             :   }
     275           2 :   gpr_asprintf(&name, "HTTP:POST:%s:%s", request->host, request->path);
     276           2 :   internal_request_begin(
     277             :       exec_ctx, context, pollset, request, deadline, on_response, user_data,
     278             :       name, grpc_httpcli_format_post_request(request, body_bytes, body_size));
     279           2 :   gpr_free(name);
     280             : }
     281             : 
     282          29 : void grpc_httpcli_set_override(grpc_httpcli_get_override get,
     283             :                                grpc_httpcli_post_override post) {
     284          29 :   g_get_override = get;
     285          29 :   g_post_override = post;
     286          29 : }

Generated by: LCOV version 1.11