• 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 "src/core/util/http_client/format_request.h"
20 
21 #include <grpc/slice.h>
22 #include <grpc/support/port_platform.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29 
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/str_format.h"
32 #include "absl/strings/str_join.h"
33 #include "absl/strings/string_view.h"
34 #include "src/core/util/http_client/httpcli.h"
35 
fill_common_header(const grpc_http_request * request,const char * host,const char * path,bool connection_close,std::vector<std::string> * buf)36 static void fill_common_header(const grpc_http_request* request,
37                                const char* host, const char* path,
38                                bool connection_close,
39                                std::vector<std::string>* buf) {
40   buf->push_back(path);
41   buf->push_back(" HTTP/1.1\r\n");
42   buf->push_back("Host: ");
43   buf->push_back(host);
44   buf->push_back("\r\n");
45   if (connection_close) buf->push_back("Connection: close\r\n");
46   buf->push_back("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n");
47   // user supplied headers
48   for (size_t i = 0; i < request->hdr_count; i++) {
49     buf->push_back(request->hdrs[i].key);
50     buf->push_back(": ");
51     buf->push_back(request->hdrs[i].value);
52     buf->push_back("\r\n");
53   }
54 }
55 
grpc_httpcli_format_get_request(const grpc_http_request * request,const char * host,const char * path)56 grpc_slice grpc_httpcli_format_get_request(const grpc_http_request* request,
57                                            const char* host, const char* path) {
58   std::vector<std::string> out;
59   out.push_back("GET ");
60   fill_common_header(request, host, path, true, &out);
61   out.push_back("\r\n");
62   std::string req = absl::StrJoin(out, "");
63   return grpc_slice_from_copied_buffer(req.data(), req.size());
64 }
65 
grpc_httpcli_format_post_request(const grpc_http_request * request,const char * host,const char * path)66 grpc_slice grpc_httpcli_format_post_request(const grpc_http_request* request,
67                                             const char* host,
68                                             const char* path) {
69   std::vector<std::string> out;
70   out.push_back("POST ");
71   fill_common_header(request, host, path, true, &out);
72   if (request->body != nullptr) {
73     bool has_content_type = false;
74     for (size_t i = 0; i < request->hdr_count; i++) {
75       if (strcmp(request->hdrs[i].key, "Content-Type") == 0) {
76         has_content_type = true;
77         break;
78       }
79     }
80     if (!has_content_type) {
81       out.push_back("Content-Type: text/plain\r\n");
82     }
83     out.push_back(
84         absl::StrFormat("Content-Length: %lu\r\n",
85                         static_cast<unsigned long>(request->body_length)));
86   }
87   out.push_back("\r\n");
88   std::string req = absl::StrJoin(out, "");
89   if (request->body != nullptr) {
90     absl::StrAppend(&req,
91                     absl::string_view(request->body, request->body_length));
92   }
93   return grpc_slice_from_copied_buffer(req.data(), req.size());
94 }
95 
grpc_httpcli_format_put_request(const grpc_http_request * request,const char * host,const char * path)96 grpc_slice grpc_httpcli_format_put_request(const grpc_http_request* request,
97                                            const char* host, const char* path) {
98   std::vector<std::string> out;
99   out.push_back("PUT ");
100   fill_common_header(request, host, path, true, &out);
101   if (request->body != nullptr) {
102     bool has_content_type = false;
103     for (size_t i = 0; i < request->hdr_count; i++) {
104       if (strcmp(request->hdrs[i].key, "Content-Type") == 0) {
105         has_content_type = true;
106         break;
107       }
108     }
109     if (!has_content_type) {
110       out.push_back("Content-Type: text/plain\r\n");
111     }
112     out.push_back(
113         absl::StrFormat("Content-Length: %lu\r\n",
114                         static_cast<unsigned long>(request->body_length)));
115   }
116   out.push_back("\r\n");
117   std::string req = absl::StrJoin(out, "");
118   if (request->body != nullptr) {
119     absl::StrAppend(&req,
120                     absl::string_view(request->body, request->body_length));
121   }
122   return grpc_slice_from_copied_buffer(req.data(), req.size());
123 }
124 
grpc_httpcli_format_connect_request(const grpc_http_request * request,const char * host,const char * path)125 grpc_slice grpc_httpcli_format_connect_request(const grpc_http_request* request,
126                                                const char* host,
127                                                const char* path) {
128   std::vector<std::string> out;
129   out.push_back("CONNECT ");
130   fill_common_header(request, host, path, false, &out);
131   out.push_back("\r\n");
132   std::string req = absl::StrJoin(out, "");
133   return grpc_slice_from_copied_buffer(req.data(), req.size());
134 }
135