MCPcopy Create free account
hub / github.com/Achain-Dev/Achain / lualib_http_request

Function lualib_http_request

src/Chain/libraries/glua/lhttplib.cpp:152–317  ·  view source on GitHub ↗

发起http请求, http.request(method, url, body, headers)

Source from the content-addressed store, hash-verified

150
151// 发起http请求, http.request(method, url, body, headers)
152static int lualib_http_request(lua_State *L)
153{
154 auto method = luaL_checkstring(L, 1);
155 auto url = luaL_checkstring(L, 2);
156 std::string method_str(method);
157 easy_toupper(method_str);
158 size_t body_len = 0;
159 auto body = luaL_tolstring(L, 3, &body_len);
160 /*
161 auto headers_table_value = lua_type_to_storage_value_type(L, 4);
162 if(headers_table_value.type != GluaStorageValueType::LVALUE_TABLE)
163 {
164 global_glua_chain_api->throw_exception(L, THINKYOUNG_API_SIMPLE_ERROR, "headers must be table");
165 return 0;
166 }
167 */
168 // auto headers_table = headers_table_value.value.table_value;
169 std::string url_str(url);
170 std::wstring url_wstr(url_str.begin(), url_str.end());
171 auto uri = glua::net::Uri::parse(url_wstr);
172 int result_count = lualib_net_connect_impl(L, uri.host_str(), uri.port_int());
173 auto *socket = (TcpSocket*)lua_touserdata(L, -1);
174 lua_pop(L, result_count);
175 std::vector<char> buf;
176 std::stringstream ss;
177 ss << method_str << " " << uri.path_str();
178 if (uri.query_string.length() > 0)
179 ss << "?" << uri.query_string_str();
180 ss << " HTTP/1.1\r\n";
181 /*
182 for(const auto &p : *headers_table)
183 {
184 if (p.second.type == GluaStorageValueType::LVALUE_STRING)
185 ss << p.first << ":" << p.second.value.string_value << "\r\n";
186 else if(p.second.type == GluaStorageValueType::LVALUE_INTEGER)
187 ss << p.first << ":" << p.second.value.int_value << "\r\n";
188 else if (p.second.type == GluaStorageValueType::LVALUE_NUMBER)
189 ss << p.first << ":" << p.second.value.number_value << "\r\n";
190 else if (p.second.type == GluaStorageValueType::LVALUE_BOOLEAN)
191 ss << p.first << ":" << p.second.value.bool_value << "\r\n";
192 }
193 */
194 std::list<const void*> ignored_nested;
195 ss << "Host:" << uri.host_str() << "\r\n";
196 ss << "Connection:keep-alive" << "\r\n";
197 luaL_traverse_table_with_nested(L, 4, response_headers_traverser, &ss, ignored_nested, 0);
198 ss << "\r\n";
199 std::string data_str = ss.str();
200 for (size_t i = 0; i < data_str.length(); ++i)
201 buf.push_back(data_str[i]);
202 for (size_t i = 0; i < body_len; ++i)
203 buf.push_back(body[i]);
204 result_count = lualib_net_write_impl(L, socket, buf);
205 if (result_count > 0)
206 lua_pop(L, result_count);
207 // read response
208 auto *ctx = new HttpContext();
209 ctx->socket = socket;

Callers

nothing calls this directly

Calls 15

easy_toupperFunction · 0.85
luaL_tolstringFunction · 0.85
lualib_net_connect_implFunction · 0.85
lua_touserdataFunction · 0.85
lualib_net_write_implFunction · 0.85
lua_tolstringFunction · 0.85
is_any_ofFunction · 0.85
stoiFunction · 0.85
lualib_net_read_implFunction · 0.85
lua_pushlightuserdataFunction · 0.85

Tested by

no test coverage detected