mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
Update Fetch() to POST if body is provided (#211)
This commit is contained in:
parent
e99a4dcc8c
commit
98c674d915
1 changed files with 14 additions and 6 deletions
|
@ -3438,14 +3438,14 @@ static int LuaFetch(lua_State *L) {
|
||||||
bool usessl;
|
bool usessl;
|
||||||
uint32_t ip;
|
uint32_t ip;
|
||||||
struct Url url;
|
struct Url url;
|
||||||
int t, ret, sock;
|
int t, ret, sock, method;
|
||||||
char *host, *port;
|
char *host, *port;
|
||||||
struct TlsBio *bio;
|
struct TlsBio *bio;
|
||||||
struct Buffer inbuf;
|
struct Buffer inbuf;
|
||||||
struct addrinfo *addr;
|
struct addrinfo *addr;
|
||||||
struct HttpMessage msg;
|
struct HttpMessage msg;
|
||||||
struct HttpUnchunker u;
|
struct HttpUnchunker u;
|
||||||
const char *urlarg, *request;
|
const char *urlarg, *request, *body;
|
||||||
size_t urlarglen, requestlen;
|
size_t urlarglen, requestlen;
|
||||||
size_t g, i, n, hdrsize, paylen;
|
size_t g, i, n, hdrsize, paylen;
|
||||||
struct addrinfo hints = {.ai_family = AF_INET,
|
struct addrinfo hints = {.ai_family = AF_INET,
|
||||||
|
@ -3457,6 +3457,14 @@ static int LuaFetch(lua_State *L) {
|
||||||
* Get args.
|
* Get args.
|
||||||
*/
|
*/
|
||||||
urlarg = luaL_checklstring(L, 1, &urlarglen);
|
urlarg = luaL_checklstring(L, 1, &urlarglen);
|
||||||
|
if (lua_isnoneornil(L, 2)) {
|
||||||
|
body = "";
|
||||||
|
method = kHttpGet;
|
||||||
|
} else {
|
||||||
|
size_t bodylen;
|
||||||
|
body = luaL_checklstring(L, 2, &bodylen);
|
||||||
|
method = kHttpPost;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse URL.
|
* Parse URL.
|
||||||
|
@ -3508,12 +3516,12 @@ static int LuaFetch(lua_State *L) {
|
||||||
/*
|
/*
|
||||||
* Create HTTP message.
|
* Create HTTP message.
|
||||||
*/
|
*/
|
||||||
request = gc(xasprintf("GET %s HTTP/1.1\r\n"
|
request = gc(xasprintf("%s %s HTTP/1.1\r\n"
|
||||||
"Host: %s:%s\r\n"
|
"Host: %s:%s\r\n"
|
||||||
"Connection: close\r\n"
|
"Connection: close\r\n"
|
||||||
"User-Agent: %s\r\n"
|
"User-Agent: %s\r\n"
|
||||||
"\r\n",
|
"\r\n%s",
|
||||||
gc(EncodeUrl(&url, 0)), host, port, brand));
|
kHttpMethod[method], gc(EncodeUrl(&url, 0)), host, port, brand, body));
|
||||||
requestlen = strlen(request);
|
requestlen = strlen(request);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -3571,7 +3579,7 @@ static int LuaFetch(lua_State *L) {
|
||||||
/*
|
/*
|
||||||
* Send HTTP Message.
|
* Send HTTP Message.
|
||||||
*/
|
*/
|
||||||
DEBUGF("client sending");
|
DEBUGF("client sending %s request", kHttpMethod[method]);
|
||||||
if (usessl) {
|
if (usessl) {
|
||||||
ret = mbedtls_ssl_write(&sslcli, request, requestlen);
|
ret = mbedtls_ssl_write(&sslcli, request, requestlen);
|
||||||
if (ret != requestlen) {
|
if (ret != requestlen) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue