read = CALL_MUX_WITH_RET(conn->mux, rcv_buf(hs->sc, &hs->req, max, 0));
cur_read += read;
if (!htx_expect_more(htxbuf(&hs->req))) {
- fin = 1;
- hs->flags |= HS_ST_HTTP_EOM_RCVD;
- break;
+ fin = 1;
+ hs->flags |= HS_ST_HTTP_EOM_RCVD;
+ hs->req_eom_ns = now_ns;
+ break;
}
if (!read)
struct htx *htx;
unsigned int flags = HTX_SL_F_IS_RESP | HTX_SL_F_XFER_LEN | (!(hs->flags & HS_ST_OPT_CHUNK_RES) ? HTX_SL_F_CLEN : 0);
struct htx_sl *sl;
+ ullong wait_us;
char *end;
TRACE_ENTER(HS_EV_HSTRM_RESP, hs);
end = ultoa_o(hs->req_code, trash.area, trash.size);
if (!end)
goto err;
+
buf = hstream_get_buf(hs, &hs->res);
if (!buf) {
TRACE_ERROR("could not allocate response buffer", HS_EV_HSTRM_RESP, hs);
goto err;
}
- /* XXX TODO time? XXX */
+ /* time from the stream creation to the end of the request, in
+ * microseconds, or -1 if the request was not fully received yet
+ * (e.g. its body is drained after the response with ?A=1).
+ */
chunk_reset(&trash);
- if (!chunk_strcat(&trash, "time=0 ms") ||
- !htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) {
+ if (!chunk_strcat(&trash, "time=")) {
+ TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ if (hs->flags & HS_ST_HTTP_EOM_RCVD) {
+ end = ulltoa((hs->req_eom_ns - hs->accept_ns) / 1000,
+ trash.area + trash.data, trash.size - trash.data);
+ if (!end)
+ goto err;
+
+ trash.data = end - trash.area;
+ }
+ else if (!chunk_strcat(&trash, "-1")) {
+ TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ if (!chunk_strcat(&trash, " us")) {
+ TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ if (!htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) {
TRACE_ERROR("could not add x-req HTX header", HS_EV_HSTRM_RESP, hs);
- goto err;
+ goto err;
}
- /* XXX TODO time? XXX */
chunk_reset(&trash);
if (!chunk_strcat(&trash, "id=dummy, code=")) {
TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
goto err;
}
+
end = ultoa_o(hs->req_code, trash.area + trash.data, trash.size - trash.data);
if (!end)
goto err;
+
trash.data = end - trash.area;
if (!chunk_strcat(&trash, ((hs->flags & HS_ST_OPT_NO_CACHE) ? ", cache=0" : ", cache=1"))) {
TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
goto err;
}
+
if ((hs->flags & HS_ST_OPT_CHUNK_RES) && !chunk_strcat(&trash, ", chunked,")) {
TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
goto err;
}
+
if (!chunk_strcat(&trash, " size=")) {
TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
goto err;
}
+
end = ultoa_o(hs->req_size, trash.area + trash.data, trash.size - trash.data);
if (!end)
goto err;
+
trash.data = end - trash.area;
+ /* the delay requested with ?t= followed by the one really spent
+ * waiting, both in microseconds.
+ */
+ if (!chunk_strcat(&trash, ", time=")) {
+ TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ wait_us = 0;
+ if (tick_isset(hs->res_wait))
+ wait_us = (ullong)TICKS_TO_MS(hs->res_wait) * 1000;
+ end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data);
+ if (!end)
+ goto err;
- if (!chunk_strcat(&trash, ", time=0 ms (0 real)") ||
- !htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) {
+ trash.data = end - trash.area;
+ if (!chunk_strcat(&trash, " us (")) {
+ TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ wait_us = 0;
+ if (tick_isset(hs->res_time))
+ wait_us = (now_ns - hs->wait_ns) / 1000;
+ end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data);
+ if (!end)
+ goto err;
+
+ trash.data = end - trash.area;
+ if (!chunk_strcat(&trash, " real)")) {
+ TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+ goto err;
+ }
+
+ if (!htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) {
TRACE_ERROR("could not add x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
- goto err;
+ goto err;
}
if (!htx_add_endof(htx, HTX_BLK_EOH)) {
/* The request body has always been fully received */
TRACE_STATE("no more expected data", HS_EV_HSTRM_RESP, hs);
hs->flags |= HS_ST_HTTP_EOM_RCVD;
+ hs->req_eom_ns = now_ns;
}
if (hstream_must_drain(hs)) {
if (tick_isset(hs->res_wait)) {
TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs);
hs->res_time = tick_add(now_ms, hs->res_wait);
+ hs->wait_ns = now_ns;
task_schedule(t, hs->res_time);
goto leave;
}
if (tick_isset(hs->res_wait) && !tick_isset(hs->res_time)) {
TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs);
hs->res_time = tick_add(now_ms, hs->res_wait);
+ hs->wait_ns = now_ns;
task_schedule(t, hs->res_time);
goto leave;
}
hs->res_wait = TICK_ETERNITY;
hs->res_time = TICK_ETERNITY;
hs->req_meth = HTTP_METH_OTHER;
+ hs->accept_ns = now_ns;
+ hs->req_eom_ns = 0;
+ hs->wait_ns = 0;
if (sc_conn(sc)) {
const struct mux_ops *mux = sc_mux_ops(sc);