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 <grpc/slice.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/string_util.h>
30 #include "src/core/lib/gpr/string.h"
31
fill_common_header(const grpc_httpcli_request * request,gpr_strvec * buf,bool connection_close)32 static void fill_common_header(const grpc_httpcli_request* request,
33 gpr_strvec* buf, bool connection_close) {
34 size_t i;
35 gpr_strvec_add(buf, gpr_strdup(request->http.path));
36 gpr_strvec_add(buf, gpr_strdup(" HTTP/1.0\r\n"));
37 /* just in case some crazy server really expects HTTP/1.1 */
38 gpr_strvec_add(buf, gpr_strdup("Host: "));
39 gpr_strvec_add(buf, gpr_strdup(request->host));
40 gpr_strvec_add(buf, gpr_strdup("\r\n"));
41 if (connection_close)
42 gpr_strvec_add(buf, gpr_strdup("Connection: close\r\n"));
43 gpr_strvec_add(buf,
44 gpr_strdup("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"));
45 /* user supplied headers */
46 for (i = 0; i < request->http.hdr_count; i++) {
47 gpr_strvec_add(buf, gpr_strdup(request->http.hdrs[i].key));
48 gpr_strvec_add(buf, gpr_strdup(": "));
49 gpr_strvec_add(buf, gpr_strdup(request->http.hdrs[i].value));
50 gpr_strvec_add(buf, gpr_strdup("\r\n"));
51 }
52 }
53
grpc_httpcli_format_get_request(const grpc_httpcli_request * request)54 grpc_slice grpc_httpcli_format_get_request(
55 const grpc_httpcli_request* request) {
56 gpr_strvec out;
57 char* flat;
58 size_t flat_len;
59
60 gpr_strvec_init(&out);
61 gpr_strvec_add(&out, gpr_strdup("GET "));
62 fill_common_header(request, &out, true);
63 gpr_strvec_add(&out, gpr_strdup("\r\n"));
64
65 flat = gpr_strvec_flatten(&out, &flat_len);
66 gpr_strvec_destroy(&out);
67
68 return grpc_slice_new(flat, flat_len, gpr_free);
69 }
70
grpc_httpcli_format_post_request(const grpc_httpcli_request * request,const char * body_bytes,size_t body_size)71 grpc_slice grpc_httpcli_format_post_request(const grpc_httpcli_request* request,
72 const char* body_bytes,
73 size_t body_size) {
74 gpr_strvec out;
75 char* tmp;
76 size_t out_len;
77 size_t i;
78
79 gpr_strvec_init(&out);
80
81 gpr_strvec_add(&out, gpr_strdup("POST "));
82 fill_common_header(request, &out, true);
83 if (body_bytes) {
84 uint8_t has_content_type = 0;
85 for (i = 0; i < request->http.hdr_count; i++) {
86 if (strcmp(request->http.hdrs[i].key, "Content-Type") == 0) {
87 has_content_type = 1;
88 break;
89 }
90 }
91 if (!has_content_type) {
92 gpr_strvec_add(&out, gpr_strdup("Content-Type: text/plain\r\n"));
93 }
94 gpr_asprintf(&tmp, "Content-Length: %lu\r\n",
95 static_cast<unsigned long>(body_size));
96 gpr_strvec_add(&out, tmp);
97 }
98 gpr_strvec_add(&out, gpr_strdup("\r\n"));
99 tmp = gpr_strvec_flatten(&out, &out_len);
100 gpr_strvec_destroy(&out);
101
102 if (body_bytes) {
103 tmp = static_cast<char*>(gpr_realloc(tmp, out_len + body_size));
104 memcpy(tmp + out_len, body_bytes, body_size);
105 out_len += body_size;
106 }
107
108 return grpc_slice_new(tmp, out_len, gpr_free);
109 }
110
grpc_httpcli_format_connect_request(const grpc_httpcli_request * request)111 grpc_slice grpc_httpcli_format_connect_request(
112 const grpc_httpcli_request* request) {
113 gpr_strvec out;
114 gpr_strvec_init(&out);
115 gpr_strvec_add(&out, gpr_strdup("CONNECT "));
116 fill_common_header(request, &out, false);
117 gpr_strvec_add(&out, gpr_strdup("\r\n"));
118 size_t flat_len;
119 char* flat = gpr_strvec_flatten(&out, &flat_len);
120 gpr_strvec_destroy(&out);
121 return grpc_slice_new(flat, flat_len, gpr_free);
122 }
123