[core] reusable code for r->state strings

convenience macros/functions for display purposes

(historical labels from mod_status are preserved for compatibility)
master
Glenn Strauss 9 months ago
parent 5471a4ba55
commit 5e0d82dc8f

@ -956,37 +956,17 @@ connection *connection_accepted(server *srv, const server_socket *srv_socket, so
}
__attribute_cold__
__attribute_const__
static const char *
connection_get_state (request_state_t state)
{
switch (state) {
case CON_STATE_CONNECT: return "connect";
case CON_STATE_READ: return "read";
case CON_STATE_READ_POST: return "readpost";
case CON_STATE_WRITE: return "write";
case CON_STATE_CLOSE: return "close";
case CON_STATE_ERROR: return "error";
case CON_STATE_HANDLE_REQUEST: return "handle-req";
case CON_STATE_REQUEST_START: return "req-start";
case CON_STATE_REQUEST_END: return "req-end";
case CON_STATE_RESPONSE_START: return "resp-start";
case CON_STATE_RESPONSE_END: return "resp-end";
default: return "(unknown)";
}
}
__attribute_cold__
__attribute_noinline__
__attribute_nonnull__()
static void
connection_log_state (const request_st * const r, const char * const tag)
{
buffer * const tb = r->tmp_buf;
buffer_clear(tb);
http_request_state_append(tb, r->state);
log_error(r->conf.errh, __FILE__, __LINE__,
"fd:%d id:%d state:%s%s", r->con->fd, r->h2id,
connection_get_state(r->state), tag);
"fd:%d id:%d state:%s%s", r->con->fd, r->h2id, tb->ptr, tag);
}

@ -134,61 +134,6 @@ SETDEFAULTS_FUNC(mod_status_set_defaults) {
}
__attribute_noinline__
static void
http_request_state_append (buffer * const b, request_state_t state)
{
static const struct sn { const char *s; uint32_t n; } states[] = {
{ CONST_STR_LEN("connect") }
,{ CONST_STR_LEN("req-start") }
,{ CONST_STR_LEN("read") }
,{ CONST_STR_LEN("req-end") }
,{ CONST_STR_LEN("readpost") }
,{ CONST_STR_LEN("handle-req") }
,{ CONST_STR_LEN("resp-start") }
,{ CONST_STR_LEN("write") }
,{ CONST_STR_LEN("resp-end") }
,{ CONST_STR_LEN("error") }
,{ CONST_STR_LEN("close") }
,{ CONST_STR_LEN("(unknown)") }
};
const struct sn * const p =
states + (state <= CON_STATE_CLOSE ? state : CON_STATE_CLOSE+1);
buffer_append_string_len(b, p->s, p->n);
}
__attribute_noinline__
__attribute_pure__
static const char *
http_request_state_short (request_state_t state)
{
/*((char *) returned, but caller must use only one char)*/
static const char sstates[] = ".qrQRhsWSECx";
return sstates + (state <= CON_STATE_CLOSE ? state : CON_STATE_CLOSE+1);
}
#define http_request_state_is_keep_alive(r) \
(CON_STATE_READ == (r)->state && !buffer_is_blank(&(r)->target_orig))
#define http_con_state_is_keep_alive(con) \
((con)->h2 \
? 0 == (con)->h2->rused \
: http_request_state_is_keep_alive(&(con)->request))
#define http_con_state_append(b, con) \
(http_con_state_is_keep_alive(con) \
? buffer_append_string_len((b), CONST_STR_LEN("keep-alive")) \
: http_request_state_append((b), (con)->request.state))
#define http_con_state_short(con) \
(http_con_state_is_keep_alive(con) \
? "k" \
: http_request_state_short((con)->request.state))
static void mod_status_header_append_sort(buffer *b, plugin_data *p, const char* k, size_t klen)
{
p->conf.sort

@ -19,6 +19,44 @@
#include <stdlib.h>
#include <string.h>
__attribute_cold__
__attribute_noinline__
void
http_request_state_append (buffer * const b, request_state_t state)
{
static const struct sn { const char *s; uint32_t n; } states[] = {
{ CONST_STR_LEN("connect") }
,{ CONST_STR_LEN("req-start") }
,{ CONST_STR_LEN("read") }
,{ CONST_STR_LEN("req-end") }
,{ CONST_STR_LEN("readpost") }
,{ CONST_STR_LEN("handle-req") }
,{ CONST_STR_LEN("resp-start") }
,{ CONST_STR_LEN("write") }
,{ CONST_STR_LEN("resp-end") }
,{ CONST_STR_LEN("error") }
,{ CONST_STR_LEN("close") }
,{ CONST_STR_LEN("(unknown)") }
};
const struct sn * const p =
states +((uint32_t)state <= CON_STATE_CLOSE ? state : CON_STATE_CLOSE+1);
buffer_append_string_len(b, p->s, p->n);
}
__attribute_cold__
__attribute_noinline__
__attribute_pure__
const char *
http_request_state_short (request_state_t state)
{
/*((char *) returned, but caller must use only one char)*/
static const char sstates[] = ".qrQRhsWSECx";
return
sstates+((uint32_t)state <= CON_STATE_CLOSE ? state : CON_STATE_CLOSE+1);
}
__attribute_noinline__
__attribute_nonnull__()
__attribute_pure__

@ -104,6 +104,7 @@ typedef struct {
/* the order of the items should be the same as they are processed
* read before write as we use this later e.g. <= CON_STATE_REQUEST_END */
/* NB: must sync with http_request_state_short(), http_request_state_append() */
typedef enum {
CON_STATE_CONNECT,
CON_STATE_REQUEST_START,
@ -223,4 +224,31 @@ int http_request_host_policy(buffer *b, unsigned int http_parseopts, int scheme_
int64_t li_restricted_strtoint64 (const char *v, const uint32_t vlen, const char ** const err);
/* convenience macros/functions for display purposes */
#define http_request_state_is_keep_alive(r) \
(CON_STATE_READ == (r)->state && !buffer_is_blank(&(r)->target_orig))
#define http_con_state_is_keep_alive(con) \
((con)->h2 \
? 0 == (con)->h2->rused \
: http_request_state_is_keep_alive(&(con)->request))
#define http_con_state_append(b, con) \
(http_con_state_is_keep_alive(con) \
? buffer_append_string_len((b), CONST_STR_LEN("keep-alive")) \
: http_request_state_append((b), (con)->request.state))
#define http_con_state_short(con) \
(http_con_state_is_keep_alive(con) \
? "k" \
: http_request_state_short((con)->request.state))
__attribute_pure__
const char * http_request_state_short (request_state_t state);
void http_request_state_append (buffer *b, request_state_t state);
#endif

Loading…
Cancel
Save