• 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/lib/http/httpcli.h"
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security_constants.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28 #include <grpc/support/sync.h>
29 
30 #include "src/core/lib/gpr/env.h"
31 #include "src/core/lib/iomgr/iomgr.h"
32 #include "test/core/util/port.h"
33 #include "test/core/util/subprocess.h"
34 #include "test/core/util/test_config.h"
35 
36 static int g_done = 0;
37 static grpc_httpcli_context g_context;
38 static gpr_mu* g_mu;
39 static grpc_polling_entity g_pops;
40 
n_seconds_time(int seconds)41 static grpc_millis n_seconds_time(int seconds) {
42   return grpc_timespec_to_millis_round_up(
43       grpc_timeout_seconds_to_deadline(seconds));
44 }
45 
on_finish(void * arg,grpc_error * error)46 static void on_finish(void* arg, grpc_error* error) {
47   const char* expect =
48       "<html><head><title>Hello world!</title></head>"
49       "<body><p>This is a test</p></body></html>";
50   grpc_http_response* response = static_cast<grpc_http_response*>(arg);
51   GPR_ASSERT(response);
52   gpr_log(GPR_INFO, "response status=%d error=%s", response->status,
53           grpc_error_string(error));
54   GPR_ASSERT(response->status == 200);
55   GPR_ASSERT(response->body_length == strlen(expect));
56   GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
57   gpr_mu_lock(g_mu);
58   g_done = 1;
59   GPR_ASSERT(GRPC_LOG_IF_ERROR(
60       "pollset_kick",
61       grpc_pollset_kick(grpc_polling_entity_pollset(&g_pops), nullptr)));
62   gpr_mu_unlock(g_mu);
63 }
64 
test_get(int port)65 static void test_get(int port) {
66   grpc_httpcli_request req;
67   char* host;
68   grpc_core::ExecCtx exec_ctx;
69 
70   g_done = 0;
71   gpr_log(GPR_INFO, "test_get");
72 
73   gpr_asprintf(&host, "localhost:%d", port);
74   gpr_log(GPR_INFO, "requesting from %s", host);
75 
76   memset(&req, 0, sizeof(req));
77   req.host = host;
78   req.ssl_host_override = const_cast<char*>("foo.test.google.fr");
79   req.http.path = const_cast<char*>("/get");
80   req.handshaker = &grpc_httpcli_ssl;
81 
82   grpc_http_response response;
83   memset(&response, 0, sizeof(response));
84   grpc_resource_quota* resource_quota = grpc_resource_quota_create("test_get");
85   grpc_httpcli_get(
86       &g_context, &g_pops, resource_quota, &req, n_seconds_time(15),
87       GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx),
88       &response);
89   grpc_resource_quota_unref_internal(resource_quota);
90   gpr_mu_lock(g_mu);
91   while (!g_done) {
92     grpc_pollset_worker* worker = nullptr;
93     GPR_ASSERT(GRPC_LOG_IF_ERROR(
94         "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops),
95                                           &worker, n_seconds_time(1))));
96     gpr_mu_unlock(g_mu);
97     grpc_core::ExecCtx::Get()->Flush();
98     gpr_mu_lock(g_mu);
99   }
100   gpr_mu_unlock(g_mu);
101   gpr_free(host);
102   grpc_http_response_destroy(&response);
103 }
104 
test_post(int port)105 static void test_post(int port) {
106   grpc_httpcli_request req;
107   char* host;
108   grpc_core::ExecCtx exec_ctx;
109 
110   g_done = 0;
111   gpr_log(GPR_INFO, "test_post");
112 
113   gpr_asprintf(&host, "localhost:%d", port);
114   gpr_log(GPR_INFO, "posting to %s", host);
115 
116   memset(&req, 0, sizeof(req));
117   req.host = host;
118   req.ssl_host_override = const_cast<char*>("foo.test.google.fr");
119   req.http.path = const_cast<char*>("/post");
120   req.handshaker = &grpc_httpcli_ssl;
121 
122   grpc_http_response response;
123   memset(&response, 0, sizeof(response));
124   grpc_resource_quota* resource_quota = grpc_resource_quota_create("test_post");
125   grpc_httpcli_post(
126       &g_context, &g_pops, resource_quota, &req, "hello", 5, n_seconds_time(15),
127       GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx),
128       &response);
129   grpc_resource_quota_unref_internal(resource_quota);
130   gpr_mu_lock(g_mu);
131   while (!g_done) {
132     grpc_pollset_worker* worker = nullptr;
133     GPR_ASSERT(GRPC_LOG_IF_ERROR(
134         "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops),
135                                           &worker, n_seconds_time(1))));
136     gpr_mu_unlock(g_mu);
137     grpc_core::ExecCtx::Get()->Flush();
138     gpr_mu_lock(g_mu);
139   }
140   gpr_mu_unlock(g_mu);
141   gpr_free(host);
142   grpc_http_response_destroy(&response);
143 }
144 
destroy_pops(void * p,grpc_error * error)145 static void destroy_pops(void* p, grpc_error* error) {
146   grpc_pollset_destroy(
147       grpc_polling_entity_pollset(static_cast<grpc_polling_entity*>(p)));
148 }
149 
main(int argc,char ** argv)150 int main(int argc, char** argv) {
151   grpc_closure destroyed;
152   gpr_subprocess* server;
153   char* me = argv[0];
154   char* lslash = strrchr(me, '/');
155   char* args[5];
156   int port = grpc_pick_unused_port_or_die();
157   int arg_shift = 0;
158   /* figure out where we are */
159   char* root;
160   if (lslash != nullptr) {
161     /* Hack for bazel target */
162     if (static_cast<unsigned>(lslash - me) >= (sizeof("http") - 1) &&
163         strncmp(me + (lslash - me) - sizeof("http") + 1, "http",
164                 sizeof("http") - 1) == 0) {
165       lslash = me + (lslash - me) - sizeof("http");
166     }
167     root = static_cast<char*>(
168         gpr_malloc(static_cast<size_t>(lslash - me + sizeof("/../.."))));
169     memcpy(root, me, static_cast<size_t>(lslash - me));
170     memcpy(root + (lslash - me), "/../..", sizeof("/../.."));
171   } else {
172     root = gpr_strdup(".");
173   }
174 
175   GPR_ASSERT(argc <= 2);
176   if (argc == 2) {
177     args[0] = gpr_strdup(argv[1]);
178   } else {
179     arg_shift = 1;
180     gpr_asprintf(&args[0], "%s/test/core/http/python_wrapper.sh", root);
181     gpr_asprintf(&args[1], "%s/test/core/http/test_server.py", root);
182   }
183 
184   /* Set the environment variable for the SSL certificate file */
185   char* pem_file;
186   gpr_asprintf(&pem_file, "%s/src/core/tsi/test_creds/ca.pem", root);
187   gpr_setenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR, pem_file);
188   gpr_free(pem_file);
189 
190   /* start the server */
191   args[1 + arg_shift] = const_cast<char*>("--port");
192   gpr_asprintf(&args[2 + arg_shift], "%d", port);
193   args[3 + arg_shift] = const_cast<char*>("--ssl");
194   server = gpr_subprocess_create(4 + arg_shift, (const char**)args);
195   GPR_ASSERT(server);
196   gpr_free(args[0]);
197   if (arg_shift) gpr_free(args[1]);
198   gpr_free(args[2 + arg_shift]);
199   gpr_free(root);
200 
201   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
202                                gpr_time_from_seconds(5, GPR_TIMESPAN)));
203 
204   grpc_test_init(argc, argv);
205   grpc_init();
206   grpc_httpcli_context_init(&g_context);
207   grpc_pollset* pollset =
208       static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
209   grpc_pollset_init(pollset, &g_mu);
210   g_pops = grpc_polling_entity_create_from_pollset(pollset);
211 
212   test_get(port);
213   test_post(port);
214 
215   {
216     grpc_core::ExecCtx exec_ctx;
217     grpc_httpcli_context_destroy(&g_context);
218     GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops,
219                       grpc_schedule_on_exec_ctx);
220     grpc_pollset_shutdown(grpc_polling_entity_pollset(&g_pops), &destroyed);
221   }
222   grpc_shutdown();
223 
224   gpr_free(grpc_polling_entity_pollset(&g_pops));
225 
226   gpr_subprocess_destroy(server);
227 
228   return 0;
229 }
230