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