Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions include/util_misc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file util_misc.h
* @brief Apache miscellaneous functions
*
* @defgroup APACHE_CORE_MISC Miscellaneous functions
* @ingroup APACHE_CORE
* @{
*/

#ifndef APACHE_UTIL_MISC_H
#define APACHE_UTIL_MISC_H

#ifdef __cplusplus
extern "C" {
#endif

#define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))

#ifdef __cplusplus
}
#endif

#endif /* !APACHE_UTIL_MISC_H */
/** @} */
3 changes: 2 additions & 1 deletion modules/filters/sed1.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "sed.h"
#include "apr_strings.h"
#include "regexp.h"
#include "util_misc.h"

static const char *const trans[040] = {
"\\01",
Expand Down Expand Up @@ -330,7 +331,7 @@ apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_
eval->hspend = eval->holdbuf;
eval->lcomend = &eval->genbuf[71];

for (i = 0; i < sizeof(eval->abuf) / sizeof(eval->abuf[0]); i++)
for (i = 0; i < ARRAY_LEN(eval->abuf); i++)
eval->abuf[i] = NULL;
eval->aptr = eval->abuf;
eval->pending = NULL;
Expand Down
2 changes: 0 additions & 2 deletions modules/http2/h2.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ extern const char *H2_MAGIC_TOKEN;

#define H2_STREAM_CLIENT_INITIATED(id) (id&0x01)

#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))

#define H2MAX(x,y) ((x) > (y) ? (x) : (y))
#define H2MIN(x,y) ((x) < (y) ? (x) : (y))

Expand Down
4 changes: 3 additions & 1 deletion modules/http2/h2_bucket_beam.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <http_request.h>
#include <http_log.h>

#include "util_misc.h"

#include "h2_private.h"
#include "h2_conn_ctx.h"
#include "h2_headers.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ static int h2_blist_count(h2_blist *blist)
do { \
if (APLOG_C_IS_LEVEL((c),(level))) { \
char buffer[4 * 1024]; \
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
apr_size_t len, bmax = ARRAY_LEN(buffer); \
len = bb? h2_util_bb_print(buffer, bmax, "", "", bb) : 0; \
ap_log_cerror(APLOG_MARK, (level), rv, (c), \
"BEAM[%s,%s%sdata=%ld,buckets(send/consumed)=%d/%d]: %s %s", \
Expand Down
4 changes: 3 additions & 1 deletion modules/http2/h2_c1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <http_request.h>
#include <http_ssl.h>

#include "util_misc.h"

#include "h2_private.h"
#include "h2_bucket_eos.h"
#include "h2_config.h"
Expand Down Expand Up @@ -63,7 +65,7 @@ static void h2_c1_io_bb_log(conn_rec *c, int stream_id, int level,
{
char buffer[16 * 1024];
const char *line = "(null)";
int bmax = sizeof(buffer)/sizeof(buffer[0]);
int bmax = ARRAY_LEN(buffer);
int off = 0;
apr_bucket *b;

Expand Down
4 changes: 2 additions & 2 deletions modules/http2/h2_c2_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ apr_status_t h2_c2_filter_request_in(ap_filter_t *f,
do { \
if (APLOG_C_IS_LEVEL((c),(level))) { \
char buffer[4 * 1024]; \
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
apr_size_t len, bmax = ARRAY_LEN(buffer); \
len = h2_util_bb_print(buffer, bmax, "", "", (bb)); \
ap_log_cerror(APLOG_MARK, (level), rv, (c), \
"FILTER[%s]: %s %s", \
Expand Down Expand Up @@ -800,7 +800,7 @@ static void make_chunk(conn_rec *c, h2_chunk_filter_t *fctx, apr_bucket_brigade
apr_bucket *b;
apr_size_t len;

len = (apr_size_t)apr_snprintf(buffer, H2_ALEN(buffer),
len = (apr_size_t)apr_snprintf(buffer, ARRAY_LEN(buffer),
"%"APR_UINT64_T_HEX_FMT"\r\n", (apr_uint64_t)chunk_len);
b = apr_bucket_heap_create(buffer, len, NULL, bb->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(first, b);
Expand Down
7 changes: 4 additions & 3 deletions modules/http2/h2_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <http_ssl.h>
#include <http_log.h>

#include "util_misc.h"

#include "mod_http2.h"
#include "h2_private.h"

Expand All @@ -43,7 +45,6 @@
#include "h2_session.h"
#include "h2_util.h"
#include "h2_protocol.h"
#include "mod_http2.h"

const char *h2_protocol_ids_tls[] = {
"h2", NULL
Expand Down Expand Up @@ -77,7 +78,7 @@ static const char *h2_err_descr[] = {

const char *h2_protocol_err_description(unsigned int h2_error)
{
if (h2_error < (sizeof(h2_err_descr)/sizeof(h2_err_descr[0]))) {
if (h2_error < (ARRAY_LEN(h2_err_descr))) {
return h2_err_descr[h2_error];
}
return "unknown http/2 error code";
Expand Down Expand Up @@ -395,7 +396,7 @@ static const char *RFC7540_names[] = {
"SSL3_CK_SCSV", /* TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
"SSL3_CK_FALLBACK_SCSV"
};
static size_t RFC7540_names_LEN = sizeof(RFC7540_names)/sizeof(RFC7540_names[0]);
static size_t RFC7540_names_LEN = ARRAY_LEN(RFC7540_names);


static apr_hash_t *BLCNames;
Expand Down
8 changes: 4 additions & 4 deletions modules/http2/h2_proxy_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static int on_frame_recv(nghttp2_session *ngh2, const nghttp2_frame *frame,
if (APLOGcdebug(session->c)) {
char buffer[256];

h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
h2_proxy_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03341)
"h2_proxy_session(%s): recv FRAME[%s]",
session->id, buffer);
Expand Down Expand Up @@ -343,7 +343,7 @@ static int before_frame_send(nghttp2_session *ngh2,
if (APLOGcdebug(session->c)) {
char buffer[256];

h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
h2_proxy_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03343)
"h2_proxy_session(%s): sent FRAME[%s]",
session->id, buffer);
Expand Down Expand Up @@ -801,7 +801,7 @@ static apr_status_t session_start(h2_proxy_session *session)
settings[1].value = (1 << session->window_bits_stream) - 1;

rv = nghttp2_submit_settings(session->ngh2, NGHTTP2_FLAG_NONE, settings,
H2_ALEN(settings));
ARRAY_LEN(settings));

/* If the connection window is larger than our default, trigger a WINDOW_UPDATE */
add_conn_window = ((1 << session->window_bits_connection) - 1 -
Expand Down Expand Up @@ -1178,7 +1178,7 @@ static const char *StateNames[] = {

static const char *state_name(h2_proxys_state state)
{
if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) {
if (state >= (ARRAY_LEN(StateNames)) {
return "unknown";
}
return StateNames[state];
Expand Down
2 changes: 0 additions & 2 deletions modules/http2/h2_proxy_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef h2_proxy_session_h
#define h2_proxy_session_h

#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))

#include <nghttp2/nghttp2.h>

struct h2_proxy_iqueue;
Expand Down
4 changes: 2 additions & 2 deletions modules/http2/h2_proxy_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ typedef struct {
} literal;

#define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) }
#define H2_LIT_ARGS(a) (a),H2_ALEN(a)
#define H2_LIT_ARGS(a) (a),ARRAY_LEN(a)

static literal IgnoredRequestHeaders[] = {
H2_DEF_LITERAL("upgrade"),
Expand Down Expand Up @@ -653,7 +653,7 @@ apr_status_t h2_proxy_req_make(h2_proxy_request *req, apr_pool_t *pool,
int h2_proxy_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen)
{
char scratch[128];
size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
size_t s_len = ARRAY_LEN(scratch);

switch (frame->hd.type) {
case NGHTTP2_DATA: {
Expand Down
12 changes: 7 additions & 5 deletions modules/http2/h2_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <unistd.h> /* for getpid() */
#endif

#include "util_misc.h"

#include "h2_private.h"
#include "h2.h"
#include "h2_bucket_beam.h"
Expand Down Expand Up @@ -219,7 +221,7 @@ static int on_invalid_frame_recv_cb(nghttp2_session *ngh2,
if (APLOGcdebug(session->c1)) {
char buffer[256];

h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
H2_SSSN_LOG(APLOGNO(03063), session,
"recv invalid FRAME[%s], frames=%ld/%ld (r/s)"),
Expand Down Expand Up @@ -344,7 +346,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s,
if (APLOGcdebug(session->c1)) {
char buffer[256];

h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
if (stream) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
H2_STRM_LOG(APLOGNO(10302), stream,
Expand Down Expand Up @@ -440,7 +442,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s,
char buffer[256];

h2_util_frame_print(frame, buffer,
sizeof(buffer)/sizeof(buffer[0]));
ARRAY_LEN(buffer));
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c1,
H2_SSSN_MSG(session, "on_frame_rcv %s"), buffer);
}
Expand Down Expand Up @@ -590,7 +592,7 @@ static int on_frame_send_cb(nghttp2_session *ngh2,
if (APLOGcdebug(session->c1)) {
char buffer[256];

h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
if (stream) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
H2_STRM_LOG(APLOGNO(10303), stream,
Expand Down Expand Up @@ -1369,7 +1371,7 @@ static const char *StateNames[] = {

const char *h2_session_state_str(h2_session_state state)
{
if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) {
if (state >= (ARRAY_LEN(StateNames))) {
return "unknown";
}
return StateNames[state];
Expand Down
10 changes: 6 additions & 4 deletions modules/http2/h2_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <http_protocol.h>
#include <http_ssl.h>

#include "util_misc.h"

#include <nghttp2/nghttp2.h>

#include "h2_private.h"
Expand Down Expand Up @@ -156,12 +158,12 @@ static int on_frame(h2_stream_state_t state, int frame_type,

static int on_frame_send(h2_stream_state_t state, int frame_type)
{
return on_frame(state, frame_type, trans_on_send, H2_ALEN(trans_on_send));
return on_frame(state, frame_type, trans_on_send, ARRAY_LEN(trans_on_send));
}

static int on_frame_recv(h2_stream_state_t state, int frame_type)
{
return on_frame(state, frame_type, trans_on_recv, H2_ALEN(trans_on_recv));
return on_frame(state, frame_type, trans_on_recv, ARRAY_LEN(trans_on_recv));
}

static int on_event(h2_stream* stream, h2_stream_event_t ev)
Expand All @@ -170,7 +172,7 @@ static int on_event(h2_stream* stream, h2_stream_event_t ev)
if (stream->monitor && stream->monitor->on_event) {
stream->monitor->on_event(stream->monitor->ctx, stream, ev);
}
if (ev < H2_ALEN(trans_on_event)) {
if (ev < ARRAY_LEN(trans_on_event)) {
return on_map(stream->state, trans_on_event[ev]);
}
return stream->state;
Expand All @@ -189,7 +191,7 @@ static void H2_STREAM_OUT_LOG(int lvl, h2_stream *s, const char *tag)
if (APLOG_C_IS_LEVEL(s->session->c1, lvl)) {
conn_rec *c = s->session->c1;
char buffer[4 * 1024];
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]);
apr_size_t len, bmax = ARRAY_LEN(buffer);

len = h2_util_bb_print(buffer, bmax, tag, "", s->out_buffer);
ap_log_cerror(APLOG_MARK, lvl, 0, c,
Expand Down
12 changes: 7 additions & 5 deletions modules/http2/h2_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <http_protocol.h>
#include <http_request.h>

#include "util_misc.h"

#include <nghttp2/nghttp2.h>

#include "h2.h"
Expand Down Expand Up @@ -1530,7 +1532,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
apr_psprintf(p, "%d", response->status)
};
return ngheader_create(ph, p, is_unsafe(response),
H2_ALEN(keys), keys, values, response->headers);
ARRAY_LEN(keys), keys, values, response->headers);
}

#else /* AP_HAS_RESPONSE_BUCKETS */
Expand Down Expand Up @@ -1558,7 +1560,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
apr_psprintf(p, "%d", headers->status)
};
return ngheader_create(ph, p, is_unsafe(headers),
H2_ALEN(keys), keys, values, headers->headers);
ARRAY_LEN(keys), keys, values, headers->headers);
}

#endif /* else AP_HAS_RESPONSE_BUCKETS */
Expand All @@ -1585,7 +1587,7 @@ apr_status_t h2_req_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
ap_assert(req->path);
ap_assert(req->method);

return ngheader_create(ph, p, 0, H2_ALEN(keys), keys, values, req->headers);
return ngheader_create(ph, p, 0, ARRAY_LEN(keys), keys, values, req->headers);
}

/*******************************************************************************
Expand All @@ -1599,7 +1601,7 @@ typedef struct {
} literal;

#define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) }
#define H2_LIT_ARGS(a) (a),H2_ALEN(a)
#define H2_LIT_ARGS(a) (a),ARRAY_LEN(a)

static literal IgnoredRequestHeaders[] = {
H2_DEF_LITERAL("upgrade"),
Expand Down Expand Up @@ -1769,7 +1771,7 @@ apr_status_t h2_req_add_header(apr_table_t *headers, apr_pool_t *pool,
int h2_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen)
{
char scratch[128];
size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
size_t s_len = ARRAY_LEN(scratch);

switch (frame->hd.type) {
case NGHTTP2_DATA: {
Expand Down
2 changes: 1 addition & 1 deletion modules/http2/h2_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ if (APLOG_C_IS_LEVEL(c, level)) { \
do { \
char buffer[4 * 1024]; \
const char *line = "(null)"; \
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
apr_size_t len, bmax = ARRAY_LEN(buffer); \
len = h2_util_bb_print(buffer, bmax, (tag), "", (bb)); \
ap_log_cerror(APLOG_MARK, level, 0, (c), "bb_dump(%ld): %s", \
((c)->master? (c)->master->id : (c)->id), (len? buffer : line)); \
Expand Down
Loading