return 1;
}
-static int uh_lua_send_common(lua_State *L, int chunked)
+static int uh_lua_send_common(lua_State *L, bool chunked)
{
size_t length;
static int uh_lua_send(lua_State *L)
{
- return uh_lua_send_common(L, 0);
+ return uh_lua_send_common(L, false);
}
static int uh_lua_sendc(lua_State *L)
{
- return uh_lua_send_common(L, 1);
+ return uh_lua_send_common(L, true);
}
static int uh_lua_str2str(lua_State *L, int (*xlate_func) (char *, int, const char *, int))
static void uh_lua_shutdown(struct uh_lua_state *state)
{
- close(state->rfd);
- close(state->wfd);
free(state);
}
while (state->content_length > 0)
{
/* remaining data in http head buffer ... */
- if (state->cl->httpbuf.len > 0)
+ if (cl->httpbuf.len > 0)
{
- len = min(state->content_length, state->cl->httpbuf.len);
+ len = min(state->content_length, cl->httpbuf.len);
- D("Lua: Child(%d) feed %d HTTP buffer bytes\n",
- state->cl->proc.pid, len);
+ D("Lua: Child(%d) feed %d HTTP buffer bytes\n", cl->proc.pid, len);
- memcpy(buf, state->cl->httpbuf.ptr, len);
+ memcpy(buf, cl->httpbuf.ptr, len);
- state->cl->httpbuf.len -= len;
- state->cl->httpbuf.ptr += len;
+ cl->httpbuf.len -= len;
+ cl->httpbuf.ptr += len;
}
/* read it from socket ... */
else
{
- len = uh_tcp_recv(state->cl, buf,
- min(state->content_length, sizeof(buf)));
+ len = uh_tcp_recv(cl, buf, min(state->content_length, sizeof(buf)));
if ((len < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))
break;
D("Lua: Child(%d) feed %d/%d TCP socket bytes\n",
- state->cl->proc.pid, len,
- min(state->content_length, sizeof(buf)));
+ cl->proc.pid, len, min(state->content_length, sizeof(buf)));
}
if (len)
state->content_length = 0;
/* ... write to Lua process */
- len = uh_raw_send(state->wfd, buf, len,
+ len = uh_raw_send(cl->wpipe.fd, buf, len,
cl->server->conf->script_timeout);
/* explicit EOF notification for the child */
if (state->content_length <= 0)
- close(state->wfd);
+ uh_ufd_remove(&cl->wpipe);
}
/* try to read data from child */
- while ((len = uh_raw_recv(state->rfd, buf, sizeof(buf), -1)) > 0)
+ while ((len = uh_raw_recv(cl->rpipe.fd, buf, sizeof(buf), -1)) > 0)
{
/* pass through buffer to socket */
- D("Lua: Child(%d) relaying %d normal bytes\n", state->cl->proc.pid, len);
- ensure_out(uh_tcp_send(state->cl, buf, len));
+ D("Lua: Child(%d) relaying %d normal bytes\n", cl->proc.pid, len);
+ ensure_out(uh_tcp_send(cl, buf, len));
state->data_sent = true;
}
((errno != EAGAIN) && (errno != EWOULDBLOCK) && (len == -1)))
{
D("Lua: Child(%d) presumed dead [%s]\n",
- state->cl->proc.pid, strerror(errno));
+ cl->proc.pid, strerror(errno));
goto out;
}
out:
if (!state->data_sent)
{
- if (state->cl->timeout.pending)
- uh_http_sendhf(state->cl, 502, "Bad Gateway",
+ if (cl->timeout.pending)
+ uh_http_sendhf(cl, 502, "Bad Gateway",
"The Lua process did not produce any response\n");
else
- uh_http_sendhf(state->cl, 504, "Gateway Timeout",
+ uh_http_sendhf(cl, 504, "Gateway Timeout",
"The Lua process took too long to produce a "
"response\n");
}
lua_newtable(L);
/* request method */
- switch(req->method)
- {
- case UH_HTTP_MSG_GET:
- lua_pushstring(L, "GET");
- break;
-
- case UH_HTTP_MSG_HEAD:
- lua_pushstring(L, "HEAD");
- break;
-
- case UH_HTTP_MSG_POST:
- lua_pushstring(L, "POST");
- break;
- }
-
+ lua_pushstring(L, http_methods[req->method]);
lua_setfield(L, -2, "REQUEST_METHOD");
/* request url */
}
/* http protcol version */
- lua_pushnumber(L, floor(req->version * 10) / 10);
+ lua_pushnumber(L, 0.9 + (req->version / 10.0));
lua_setfield(L, -2, "HTTP_VERSION");
- if (req->version > 1.0)
- lua_pushstring(L, "HTTP/1.1");
- else
- lua_pushstring(L, "HTTP/1.0");
-
+ lua_pushstring(L, http_versions[req->version]);
lua_setfield(L, -2, "SERVER_PROTOCOL");
if (! err_str)
err_str = "Unknown error";
- printf("HTTP/%.1f 500 Internal Server Error\r\n"
+ printf("%s 500 Internal Server Error\r\n"
"Connection: close\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %i\r\n\r\n"
"Lua raised a runtime error:\n %s\n",
- req->version, 31 + strlen(err_str), err_str);
+ http_versions[req->version],
+ 31 + strlen(err_str), err_str);
break;
default:
memset(state, 0, sizeof(*state));
- state->cl = cl;
- state->cl->proc.pid = child;
+ cl->rpipe.fd = rfd[0];
+ cl->wpipe.fd = wfd[1];
+ cl->proc.pid = child;
+
+ /* make pipe non-blocking */
+ fd_nonblock(cl->rpipe.fd);
+ fd_nonblock(cl->wpipe.fd);
/* close unneeded pipe ends */
close(rfd[1]);
}
}
- state->rfd = rfd[0];
- fd_nonblock(state->rfd);
-
- state->wfd = wfd[1];
- fd_nonblock(state->wfd);
-
cl->cb = uh_lua_socket_cb;
cl->priv = state;