mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-06 09:50:28 +00:00
Update redbean Fetch to provide Content-Length header (#97).
This commit is contained in:
parent
bb7f9c6270
commit
00e3db35df
1 changed files with 15 additions and 6 deletions
|
@ -3694,8 +3694,9 @@ static int LuaFetch(lua_State *L) {
|
||||||
struct HttpMessage msg;
|
struct HttpMessage msg;
|
||||||
struct HttpUnchunker u;
|
struct HttpUnchunker u;
|
||||||
const char *urlarg, *request, *body;
|
const char *urlarg, *request, *body;
|
||||||
size_t urlarglen, requestlen;
|
char *conlenhdr = "";
|
||||||
size_t g, i, n, hdrsize, paylen;
|
size_t urlarglen, requestlen, paylen, bodylen;
|
||||||
|
size_t g, i, n, hdrsize;
|
||||||
struct addrinfo hints = {.ai_family = AF_INET,
|
struct addrinfo hints = {.ai_family = AF_INET,
|
||||||
.ai_socktype = SOCK_STREAM,
|
.ai_socktype = SOCK_STREAM,
|
||||||
.ai_protocol = IPPROTO_TCP,
|
.ai_protocol = IPPROTO_TCP,
|
||||||
|
@ -3708,17 +3709,23 @@ static int LuaFetch(lua_State *L) {
|
||||||
if (lua_istable(L, 2)) {
|
if (lua_istable(L, 2)) {
|
||||||
lua_settop(L, 2); // discard any extra arguments
|
lua_settop(L, 2); // discard any extra arguments
|
||||||
lua_getfield(L, 2, "body");
|
lua_getfield(L, 2, "body");
|
||||||
body = luaL_optstring(L, -1, "");
|
body = luaL_optstring(L, -1, "", &bodylen);
|
||||||
lua_getfield(L, 2, "method");
|
lua_getfield(L, 2, "method");
|
||||||
method = GetHttpMethod(luaL_optstring(L, -1, ""), lua_rawlen(L, -1));
|
method = GetHttpMethod(luaL_optstring(L, -1, ""), lua_rawlen(L, -1));
|
||||||
lua_settop(L, 2); // drop all added elements to keep the stack balanced
|
lua_settop(L, 2); // drop all added elements to keep the stack balanced
|
||||||
} else if (lua_isnoneornil(L, 2)) {
|
} else if (lua_isnoneornil(L, 2)) {
|
||||||
body = "";
|
body = "";
|
||||||
|
bodylen = 0;
|
||||||
method = kHttpGet;
|
method = kHttpGet;
|
||||||
} else {
|
} else {
|
||||||
body = luaL_checkstring(L, 2);
|
body = luaL_checklstring(L, 2, &bodylen);
|
||||||
method = kHttpPost;
|
method = kHttpPost;
|
||||||
}
|
}
|
||||||
|
// provide Content-Length header unless it's zero and not expected
|
||||||
|
if (bodylen > 0 ||
|
||||||
|
!(method == kHttpGet || method == kHttpHead ||
|
||||||
|
method == kHttpTrace || method == kHttpDelete || method == kHttpConnect))
|
||||||
|
conlenhdr = gc(xasprintf("Content-Length: %s\r\n", bodylen));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse URL.
|
* Parse URL.
|
||||||
|
@ -3774,9 +3781,11 @@ static int LuaFetch(lua_State *L) {
|
||||||
"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"
|
||||||
|
"%s"
|
||||||
"\r\n%s",
|
"\r\n%s",
|
||||||
kHttpMethod[method], gc(EncodeUrl(&url, 0)), host,
|
kHttpMethod[method], gc(EncodeUrl(&url, 0)),
|
||||||
port, brand, body));
|
host, port,
|
||||||
|
brand, conlenhdr, body));
|
||||||
requestlen = strlen(request);
|
requestlen = strlen(request);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue