LCOV - code coverage report
Current view: top level - src/core/httpcli - httpcli.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 116 123 94.3 %
Date: 2015-10-10 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             :   gpr_timespec deadline;
      57             :   int have_read_byte;
      58             :   const grpc_httpcli_handshaker *handshaker;
      59             :   grpc_httpcli_response_cb on_response;
      60             :   void *user_data;
      61             :   grpc_httpcli_context *context;
      62             :   grpc_pollset *pollset;
      63             :   grpc_iomgr_object iomgr_obj;
      64             :   gpr_slice_buffer incoming;
      65             :   gpr_slice_buffer outgoing;
      66             :   grpc_closure on_read;
      67             :   grpc_closure done_write;
      68             :   grpc_closure connected;
      69             : } internal_request;
      70             : 
      71             : static grpc_httpcli_get_override g_get_override = NULL;
      72             : static grpc_httpcli_post_override g_post_override = NULL;
      73             : 
      74        2904 : static void plaintext_handshake(grpc_exec_ctx *exec_ctx, void *arg,
      75             :                                 grpc_endpoint *endpoint, const char *host,
      76             :                                 void (*on_done)(grpc_exec_ctx *exec_ctx,
      77             :                                                 void *arg,
      78             :                                                 grpc_endpoint *endpoint)) {
      79        2904 :   on_done(exec_ctx, arg, endpoint);
      80        2904 : }
      81             : 
      82             : const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http",
      83             :                                                         plaintext_handshake};
      84             : 
      85        2915 : void grpc_httpcli_context_init(grpc_httpcli_context *context) {
      86        2915 :   grpc_pollset_set_init(&context->pollset_set);
      87        2915 : }
      88             : 
      89        2914 : void grpc_httpcli_context_destroy(grpc_httpcli_context *context) {
      90        2914 :   grpc_pollset_set_destroy(&context->pollset_set);
      91        2914 : }
      92             : 
      93             : static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req);
      94             : 
      95        2904 : static void finish(grpc_exec_ctx *exec_ctx, internal_request *req,
      96             :                    int success) {
      97        2904 :   grpc_pollset_set_del_pollset(exec_ctx, &req->context->pollset_set,
      98             :                                req->pollset);
      99        2904 :   req->on_response(exec_ctx, req->user_data, success ? &req->parser.r : NULL);
     100        2904 :   grpc_httpcli_parser_destroy(&req->parser);
     101        2904 :   if (req->addresses != NULL) {
     102        2904 :     grpc_resolved_addresses_destroy(req->addresses);
     103             :   }
     104        2904 :   if (req->ep != NULL) {
     105        2904 :     grpc_endpoint_destroy(exec_ctx, req->ep);
     106             :   }
     107        2904 :   gpr_slice_unref(req->request_text);
     108        2904 :   gpr_free(req->host);
     109        2904 :   grpc_iomgr_unregister_object(&req->iomgr_obj);
     110        2904 :   gpr_slice_buffer_destroy(&req->incoming);
     111        2904 :   gpr_slice_buffer_destroy(&req->outgoing);
     112        2904 :   gpr_free(req);
     113        2904 : }
     114             : 
     115             : static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success);
     116             : 
     117       15019 : static void do_read(grpc_exec_ctx *exec_ctx, internal_request *req) {
     118       15019 :   grpc_endpoint_read(exec_ctx, req->ep, &req->incoming, &req->on_read);
     119       15019 : }
     120             : 
     121       15019 : static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
     122       15019 :   internal_request *req = user_data;
     123             :   size_t i;
     124             : 
     125       27134 :   for (i = 0; i < req->incoming.count; i++) {
     126       12115 :     if (GPR_SLICE_LENGTH(req->incoming.slices[i])) {
     127       12115 :       req->have_read_byte = 1;
     128       12115 :       if (!grpc_httpcli_parser_parse(&req->parser, req->incoming.slices[i])) {
     129           0 :         finish(exec_ctx, req, 0);
     130       15019 :         return;
     131             :       }
     132             :     }
     133             :   }
     134             : 
     135       15019 :   if (success) {
     136       12115 :     do_read(exec_ctx, req);
     137        2904 :   } else if (!req->have_read_byte) {
     138           0 :     next_address(exec_ctx, req);
     139             :   } else {
     140        2904 :     finish(exec_ctx, req, grpc_httpcli_parser_eof(&req->parser));
     141             :   }
     142             : }
     143             : 
     144        2904 : static void on_written(grpc_exec_ctx *exec_ctx, internal_request *req) {
     145        2904 :   do_read(exec_ctx, req);
     146        2904 : }
     147             : 
     148        2904 : static void done_write(grpc_exec_ctx *exec_ctx, void *arg, int success) {
     149        2904 :   internal_request *req = arg;
     150        2904 :   if (success) {
     151        2904 :     on_written(exec_ctx, req);
     152             :   } else {
     153           0 :     next_address(exec_ctx, req);
     154             :   }
     155        2904 : }
     156             : 
     157        2904 : static void start_write(grpc_exec_ctx *exec_ctx, internal_request *req) {
     158        2904 :   gpr_slice_ref(req->request_text);
     159        2904 :   gpr_slice_buffer_add(&req->outgoing, req->request_text);
     160        2904 :   grpc_endpoint_write(exec_ctx, req->ep, &req->outgoing, &req->done_write);
     161        2904 : }
     162             : 
     163        2904 : static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
     164             :                               grpc_endpoint *ep) {
     165        2904 :   internal_request *req = arg;
     166             : 
     167        2904 :   if (!ep) {
     168           0 :     next_address(exec_ctx, req);
     169        2904 :     return;
     170             :   }
     171             : 
     172        2904 :   req->ep = ep;
     173        2904 :   start_write(exec_ctx, req);
     174             : }
     175             : 
     176        2904 : static void on_connected(grpc_exec_ctx *exec_ctx, void *arg, int success) {
     177        2904 :   internal_request *req = arg;
     178             : 
     179        2904 :   if (!req->ep) {
     180           0 :     next_address(exec_ctx, req);
     181        2904 :     return;
     182             :   }
     183        2904 :   req->handshaker->handshake(exec_ctx, req, req->ep, req->host,
     184             :                              on_handshake_done);
     185             : }
     186             : 
     187        2904 : static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req) {
     188             :   grpc_resolved_address *addr;
     189        2904 :   if (req->next_address == req->addresses->naddrs) {
     190           0 :     finish(exec_ctx, req, 0);
     191        2904 :     return;
     192             :   }
     193        2904 :   addr = &req->addresses->addrs[req->next_address++];
     194        2904 :   grpc_closure_init(&req->connected, on_connected, req);
     195        5808 :   grpc_tcp_client_connect(
     196        2904 :       exec_ctx, &req->connected, &req->ep, &req->context->pollset_set,
     197        2904 :       (struct sockaddr *)&addr->addr, addr->len, req->deadline);
     198             : }
     199             : 
     200        2904 : static void on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
     201             :                         grpc_resolved_addresses *addresses) {
     202        2904 :   internal_request *req = arg;
     203        2904 :   if (!addresses) {
     204           0 :     finish(exec_ctx, req, 0);
     205        2904 :     return;
     206             :   }
     207        2904 :   req->addresses = addresses;
     208        2904 :   req->next_address = 0;
     209        2904 :   next_address(exec_ctx, req);
     210             : }
     211             : 
     212        2904 : static void internal_request_begin(
     213             :     grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     214             :     grpc_pollset *pollset, const grpc_httpcli_request *request,
     215             :     gpr_timespec deadline, grpc_httpcli_response_cb on_response,
     216             :     void *user_data, const char *name, gpr_slice request_text) {
     217        2904 :   internal_request *req = gpr_malloc(sizeof(internal_request));
     218        2904 :   memset(req, 0, sizeof(*req));
     219        2904 :   req->request_text = request_text;
     220        2904 :   grpc_httpcli_parser_init(&req->parser);
     221        2904 :   req->on_response = on_response;
     222        2904 :   req->user_data = user_data;
     223        2904 :   req->deadline = deadline;
     224        2904 :   req->handshaker =
     225        2904 :       request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
     226        2904 :   req->context = context;
     227        2904 :   req->pollset = pollset;
     228        2904 :   grpc_closure_init(&req->on_read, on_read, req);
     229        2904 :   grpc_closure_init(&req->done_write, done_write, req);
     230        2904 :   gpr_slice_buffer_init(&req->incoming);
     231        2904 :   gpr_slice_buffer_init(&req->outgoing);
     232        2904 :   grpc_iomgr_register_object(&req->iomgr_obj, name);
     233        2904 :   req->host = gpr_strdup(request->host);
     234             : 
     235        2904 :   grpc_pollset_set_add_pollset(exec_ctx, &req->context->pollset_set,
     236             :                                req->pollset);
     237        2904 :   grpc_resolve_address(request->host, req->handshaker->default_port,
     238             :                        on_resolved, req);
     239        2904 : }
     240             : 
     241        2913 : void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     242             :                       grpc_pollset *pollset,
     243             :                       const grpc_httpcli_request *request,
     244             :                       gpr_timespec deadline,
     245             :                       grpc_httpcli_response_cb on_response, void *user_data) {
     246             :   char *name;
     247        2923 :   if (g_get_override &&
     248          10 :       g_get_override(exec_ctx, request, deadline, on_response, user_data)) {
     249        2923 :     return;
     250             :   }
     251        2903 :   gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path);
     252        2903 :   internal_request_begin(exec_ctx, context, pollset, request, deadline,
     253             :                          on_response, user_data, name,
     254             :                          grpc_httpcli_format_get_request(request));
     255        2903 :   gpr_free(name);
     256             : }
     257             : 
     258           3 : void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
     259             :                        grpc_pollset *pollset,
     260             :                        const grpc_httpcli_request *request,
     261             :                        const char *body_bytes, size_t body_size,
     262             :                        gpr_timespec deadline,
     263             :                        grpc_httpcli_response_cb on_response, void *user_data) {
     264             :   char *name;
     265           5 :   if (g_post_override &&
     266           2 :       g_post_override(exec_ctx, request, body_bytes, body_size, deadline,
     267             :                       on_response, user_data)) {
     268           5 :     return;
     269             :   }
     270           1 :   gpr_asprintf(&name, "HTTP:POST:%s:%s", request->host, request->path);
     271           1 :   internal_request_begin(
     272             :       exec_ctx, context, pollset, request, deadline, on_response, user_data,
     273             :       name, grpc_httpcli_format_post_request(request, body_bytes, body_size));
     274           1 :   gpr_free(name);
     275             : }
     276             : 
     277          26 : void grpc_httpcli_set_override(grpc_httpcli_get_override get,
     278             :                                grpc_httpcli_post_override post) {
     279          26 :   g_get_override = get;
     280          26 :   g_post_override = post;
     281          26 : }

Generated by: LCOV version 1.10