LCOV - code coverage report
Current view: top level - core/httpcli - format_request.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 44 44 100.0 %
Date: 2015-12-10 22:15:08 Functions: 3 3 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/httpcli/format_request.h"
      35             : 
      36             : #include <stdarg.h>
      37             : #include <stdio.h>
      38             : #include <string.h>
      39             : 
      40             : #include "src/core/support/string.h"
      41             : #include <grpc/support/alloc.h>
      42             : #include <grpc/support/slice.h>
      43             : #include <grpc/support/string_util.h>
      44             : #include <grpc/support/useful.h>
      45             : 
      46        4344 : static void fill_common_header(const grpc_httpcli_request *request,
      47             :                                gpr_strvec *buf) {
      48             :   size_t i;
      49        4344 :   gpr_strvec_add(buf, gpr_strdup(request->path));
      50        4344 :   gpr_strvec_add(buf, gpr_strdup(" HTTP/1.0\r\n"));
      51             :   /* just in case some crazy server really expects HTTP/1.1 */
      52        4344 :   gpr_strvec_add(buf, gpr_strdup("Host: "));
      53        4344 :   gpr_strvec_add(buf, gpr_strdup(request->host));
      54        4344 :   gpr_strvec_add(buf, gpr_strdup("\r\n"));
      55        4344 :   gpr_strvec_add(buf, gpr_strdup("Connection: close\r\n"));
      56        4344 :   gpr_strvec_add(buf,
      57             :                  gpr_strdup("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"));
      58             :   /* user supplied headers */
      59        4349 :   for (i = 0; i < request->hdr_count; i++) {
      60           5 :     gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].key));
      61           5 :     gpr_strvec_add(buf, gpr_strdup(": "));
      62           5 :     gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].value));
      63           5 :     gpr_strvec_add(buf, gpr_strdup("\r\n"));
      64             :   }
      65        4344 : }
      66             : 
      67        4339 : gpr_slice grpc_httpcli_format_get_request(const grpc_httpcli_request *request) {
      68             :   gpr_strvec out;
      69             :   char *flat;
      70             :   size_t flat_len;
      71             : 
      72        4339 :   gpr_strvec_init(&out);
      73        4339 :   gpr_strvec_add(&out, gpr_strdup("GET "));
      74        4339 :   fill_common_header(request, &out);
      75        4339 :   gpr_strvec_add(&out, gpr_strdup("\r\n"));
      76             : 
      77        4339 :   flat = gpr_strvec_flatten(&out, &flat_len);
      78        4339 :   gpr_strvec_destroy(&out);
      79             : 
      80        4339 :   return gpr_slice_new(flat, flat_len, gpr_free);
      81             : }
      82             : 
      83           5 : gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request,
      84             :                                            const char *body_bytes,
      85             :                                            size_t body_size) {
      86             :   gpr_strvec out;
      87             :   char *tmp;
      88             :   size_t out_len;
      89             :   size_t i;
      90             : 
      91           5 :   gpr_strvec_init(&out);
      92             : 
      93           5 :   gpr_strvec_add(&out, gpr_strdup("POST "));
      94           5 :   fill_common_header(request, &out);
      95           5 :   if (body_bytes) {
      96           4 :     gpr_uint8 has_content_type = 0;
      97           6 :     for (i = 0; i < request->hdr_count; i++) {
      98           3 :       if (strcmp(request->hdrs[i].key, "Content-Type") == 0) {
      99           1 :         has_content_type = 1;
     100           1 :         break;
     101             :       }
     102             :     }
     103           4 :     if (!has_content_type) {
     104           3 :       gpr_strvec_add(&out, gpr_strdup("Content-Type: text/plain\r\n"));
     105             :     }
     106           4 :     gpr_asprintf(&tmp, "Content-Length: %lu\r\n", (unsigned long)body_size);
     107           4 :     gpr_strvec_add(&out, tmp);
     108             :   }
     109           5 :   gpr_strvec_add(&out, gpr_strdup("\r\n"));
     110           5 :   tmp = gpr_strvec_flatten(&out, &out_len);
     111           5 :   gpr_strvec_destroy(&out);
     112             : 
     113           5 :   if (body_bytes) {
     114           4 :     tmp = gpr_realloc(tmp, out_len + body_size);
     115           4 :     memcpy(tmp + out_len, body_bytes, body_size);
     116           4 :     out_len += body_size;
     117             :   }
     118             : 
     119           5 :   return gpr_slice_new(tmp, out_len, gpr_free);
     120             : }

Generated by: LCOV version 1.11