• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/http/format_request.h"
22 
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <vector>
28 
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_join.h"
31 
32 #include <grpc/slice.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/string_util.h>
35 #include "src/core/lib/gpr/string.h"
36 
fill_common_header(const grpc_httpcli_request * request,bool connection_close,std::vector<std::string> * buf)37 static void fill_common_header(const grpc_httpcli_request* request,
38                                bool connection_close,
39                                std::vector<std::string>* buf) {
40   buf->push_back(request->http.path);
41   buf->push_back(" HTTP/1.0\r\n");
42   /* just in case some crazy server really expects HTTP/1.1 */
43   buf->push_back("Host: ");
44   buf->push_back(request->host);
45   buf->push_back("\r\n");
46   if (connection_close) buf->push_back("Connection: close\r\n");
47   buf->push_back("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n");
48   /* user supplied headers */
49   for (size_t i = 0; i < request->http.hdr_count; i++) {
50     buf->push_back(request->http.hdrs[i].key);
51     buf->push_back(": ");
52     buf->push_back(request->http.hdrs[i].value);
53     buf->push_back("\r\n");
54   }
55 }
56 
grpc_httpcli_format_get_request(const grpc_httpcli_request * request)57 grpc_slice grpc_httpcli_format_get_request(
58     const grpc_httpcli_request* request) {
59   std::vector<std::string> out;
60   out.push_back("GET ");
61   fill_common_header(request, true, &out);
62   out.push_back("\r\n");
63   std::string req = absl::StrJoin(out, "");
64   return grpc_slice_from_copied_buffer(req.data(), req.size());
65 }
66 
grpc_httpcli_format_post_request(const grpc_httpcli_request * request,const char * body_bytes,size_t body_size)67 grpc_slice grpc_httpcli_format_post_request(const grpc_httpcli_request* request,
68                                             const char* body_bytes,
69                                             size_t body_size) {
70   std::vector<std::string> out;
71   out.push_back("POST ");
72   fill_common_header(request, true, &out);
73   if (body_bytes != nullptr) {
74     bool has_content_type = false;
75     for (size_t i = 0; i < request->http.hdr_count; i++) {
76       if (strcmp(request->http.hdrs[i].key, "Content-Type") == 0) {
77         has_content_type = true;
78         break;
79       }
80     }
81     if (!has_content_type) {
82       out.push_back("Content-Type: text/plain\r\n");
83     }
84     out.push_back(absl::StrFormat("Content-Length: %lu\r\n",
85                                   static_cast<unsigned long>(body_size)));
86   }
87   out.push_back("\r\n");
88   std::string req = absl::StrJoin(out, "");
89   if (body_bytes != nullptr) {
90     absl::StrAppend(&req, absl::string_view(body_bytes, body_size));
91   }
92   return grpc_slice_from_copied_buffer(req.data(), req.size());
93 }
94 
grpc_httpcli_format_connect_request(const grpc_httpcli_request * request)95 grpc_slice grpc_httpcli_format_connect_request(
96     const grpc_httpcli_request* request) {
97   std::vector<std::string> out;
98   out.push_back("CONNECT ");
99   fill_common_header(request, false, &out);
100   out.push_back("\r\n");
101   std::string req = absl::StrJoin(out, "");
102   return grpc_slice_from_copied_buffer(req.data(), req.size());
103 }
104