OCT 重新格式化代码

This commit is contained in:
huangxin 2022-11-08 18:09:54 +08:00
parent b05d9987f1
commit e34c25a0e3
16 changed files with 1208 additions and 975 deletions

View File

@ -1,6 +1,7 @@
# ClangFormatConfigureSource: 'clang-format-file://D:/development/c/daemon_agent/.clang-format'
Language: Cpp
AccessModifierOffset: -4
InsertBraces: true
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: Consecutive
AlignConsecutiveAssignments: Consecutive

View File

@ -123,29 +123,30 @@ XX(22, SUBSCRIBE, SUBSCRIBE) \
XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
/* RFC-5789 */ \
XX(24, PATCH, PATCH) \
XX(25, PURGE, PURGE) \
XX(25, PURGE, PURGE)
enum hw_http_method
{
enum hw_http_method {
#define XX(num, name, string) HW_HTTP_##name = num,
HW_HTTP_METHOD_MAP(XX)
#undef XX
};
#define STRLENOF(s) strlen(s)
#define SETSTRING(s,val) s.value=val; s.length=STRLENOF(val)
#define APPENDSTRING(s,val) memcpy((char*)s->value + s->length, val, STRLENOF(val)); s->length+=STRLENOF(val)
#define SETSTRING(s, val) \
s.value = val; \
s.length = STRLENOF(val)
#define APPENDSTRING(s, val) \
memcpy((char *)s->value + s->length, val, STRLENOF(val)); \
s->length += STRLENOF(val)
typedef void *hw_http_response;
typedef struct
{
typedef struct {
char *value;
size_t length;
} hw_string;
typedef struct
{
typedef struct {
char *http_listen_address;
unsigned int http_listen_port;
unsigned int thread_count;
@ -156,8 +157,7 @@ typedef struct
unsigned int max_request_size;
} configuration;
typedef struct
{
typedef struct {
unsigned short http_major;
unsigned short http_minor;
unsigned char method;
@ -166,10 +166,16 @@ typedef struct
void *headers;
hw_string *body;
size_t body_length;
enum {OK, SIZE_EXCEEDED, BAD_REQUEST, INTERNAL_ERROR} state;
enum {
OK,
SIZE_EXCEEDED,
BAD_REQUEST,
INTERNAL_ERROR
} state;
} http_request;
typedef void (HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request* request, hw_http_response* response, void* user_data);
typedef void(HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request *request, hw_http_response *response,
void *user_data);
typedef void(HAYWIRE_CALLING_CONVENTION *http_response_complete_callback)(void *user_data);
HAYWIRE_EXTERN int hw_init_from_config(char *configuration_filename);
@ -184,7 +190,8 @@ HAYWIRE_EXTERN void hw_set_http_version(hw_http_response* response, unsigned sho
HAYWIRE_EXTERN void hw_set_response_status_code(hw_http_response *response, hw_string *status_code);
HAYWIRE_EXTERN void hw_set_response_header(hw_http_response *response, hw_string *name, hw_string *value);
HAYWIRE_EXTERN void hw_set_body(hw_http_response *response, hw_string *body);
HAYWIRE_EXTERN void hw_http_response_send(hw_http_response* response, void* user_data, http_response_complete_callback callback);
HAYWIRE_EXTERN void hw_http_response_send(hw_http_response *response, void *user_data,
http_response_complete_callback callback);
HAYWIRE_EXTERN void hw_http_response_send_error(hw_http_response *response, const char *error, const char *err_msg);
HAYWIRE_EXTERN void hw_print_request_headers(http_request *request);

View File

@ -21,15 +21,17 @@
/* Strip whitespace chars off end of given string, in place. Return s. */
static char *rstrip(char *s) {
char *p = s + strlen(s);
while (p > s && isspace(*--p))
while (p > s && isspace(*--p)) {
*p = '\0';
}
return s;
}
/* Return pointer to first non-whitespace char in given string. */
static char *lskip(const char *s) {
while (*s && isspace(*s))
while (*s && isspace(*s)) {
s++;
}
return (char *)s;
}
@ -78,9 +80,10 @@ int ini_parse_file(FILE *file, int (*handler)(void *, const char *, const char *
else if (*prev_name && *start && start > line) {
/* Non-black line with leading whitespace, treat as continuation
of previous name's value (as per Python ConfigParser). */
if (!handler(user, section, prev_name, start) && !error)
if (!handler(user, section, prev_name, start) && !error) {
error = lineno;
}
}
#endif
else if (*start == '[') {
/* A "[section]" line */
@ -104,14 +107,16 @@ int ini_parse_file(FILE *file, int (*handler)(void *, const char *, const char *
name = rstrip(start);
value = lskip(end + 1);
end = find_char_or_comment(value, '\0');
if (*end == ';')
if (*end == ';') {
*end = '\0';
}
rstrip(value);
/* Valid name[=:]value pair found, call handler */
strncpy0(prev_name, name, sizeof(prev_name));
if (!handler(user, section, name, value) && !error)
if (!handler(user, section, name, value) && !error) {
error = lineno;
}
} else if (!error) {
/* No '=' or ':' found on name[=:]value line */
error = lineno;
@ -128,8 +133,9 @@ int ini_parse(const char *filename, int (*handler)(void *, const char *, const c
int error;
file = fopen(filename, "r");
if (!file)
if (!file) {
return -1;
}
error = ini_parse_file(file, handler, user);
fclose(file);
return error;

View File

@ -27,8 +27,9 @@ void ipc_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
if (tcp_nodelay) {
rc = uv_tcp_nodelay((uv_tcp_t *)ctx->server_handle, 1);
}
} else if (type == UV_NAMED_PIPE)
} else if (type == UV_NAMED_PIPE) {
rc = uv_pipe_init(loop, (uv_pipe_t *)ctx->server_handle, 0);
}
rc = uv_accept(handle, ctx->server_handle);
uv_close((uv_handle_t *)&ctx->ipc_pipe, NULL);

View File

@ -36,16 +36,18 @@ void ipc_connection_cb(uv_stream_t *ipc_pipe, int status) {
if (sc->tcp_nodelay) {
rc = uv_tcp_nodelay((uv_tcp_t *)&pc->peer_handle, 1);
}
} else if (ipc_pipe->type == UV_NAMED_PIPE)
} else if (ipc_pipe->type == UV_NAMED_PIPE) {
rc = uv_pipe_init(loop, (uv_pipe_t *)&pc->peer_handle, 1);
}
rc = uv_accept(ipc_pipe, (uv_stream_t *)&pc->peer_handle);
rc = uv_write2(
&pc->write_req, (uv_stream_t *)&pc->peer_handle, &buf, 1, (uv_stream_t *)&sc->server_handle, ipc_write_cb);
if (--sc->num_connects == 0)
if (--sc->num_connects == 0) {
uv_close((uv_handle_t *)ipc_pipe, NULL);
}
}
extern void print_configuration();
/* Set up an IPC pipe server that hands out listen sockets to the worker
@ -81,13 +83,15 @@ void start_connection_dispatching(uv_handle_type type, unsigned int num_servers,
rc = uv_pipe_bind(&ctx.ipc_pipe, "HAYWIRE_CONNECTION_DISPATCH_PIPE_NAME");
rc = uv_listen((uv_stream_t *)&ctx.ipc_pipe, listen_backlog, ipc_connection_cb);
for (i = 0; i < num_servers; i++)
for (i = 0; i < num_servers; i++) {
uv_sem_post(&servers[i].semaphore);
}
rc = uv_run(loop, UV_RUN_DEFAULT);
uv_close((uv_handle_t *)&ctx.server_handle, NULL);
rc = uv_run(loop, UV_RUN_DEFAULT);
for (i = 0; i < num_servers; i++)
for (i = 0; i < num_servers; i++) {
uv_sem_wait(&servers[i].semaphore);
}
}

View File

@ -758,10 +758,12 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
}
}
if (CURRENT_STATE() == s_header_field)
if (CURRENT_STATE() == s_header_field) {
header_field_mark = data;
if (CURRENT_STATE() == s_header_value)
}
if (CURRENT_STATE() == s_header_value) {
header_value_mark = data;
}
switch (CURRENT_STATE()) {
case s_req_path:
case s_req_schema:
@ -786,8 +788,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
for (p = data; p != data + len; p++) {
ch = *p;
if (PARSING_HEADER(CURRENT_STATE()))
if (PARSING_HEADER(CURRENT_STATE())) {
COUNT_HEADER_SIZE(1);
}
switch (CURRENT_STATE()) {
@ -795,15 +798,17 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
/* this state is used after a 'Connection: close' message
* the parser will error out if it reads another message
*/
if (LIKELY(ch == CR || ch == LF))
if (LIKELY(ch == CR || ch == LF)) {
break;
}
SET_ERRNO(HPE_CLOSED_CONNECTION);
goto error;
case s_start_req_or_res: {
if (ch == CR || ch == LF)
if (ch == CR || ch == LF) {
break;
}
parser->flags = 0;
parser->content_length = ULLONG_MAX;
@ -1028,8 +1033,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
break;
case s_start_req: {
if (ch == CR || ch == LF)
if (ch == CR || ch == LF) {
break;
}
parser->flags = 0;
parser->content_length = ULLONG_MAX;
@ -1177,8 +1183,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
}
case s_req_spaces_before_url: {
if (ch == ' ')
if (ch == ' ') {
break;
}
MARK(url);
if (parser->method == HTTP_CONNECT) {
@ -1419,8 +1426,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
ch = *p;
c = TOKEN(ch);
if (!c)
if (!c) {
break;
}
switch (parser->header_state) {
case h_general:
@ -1511,8 +1519,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
case h_content_length:
case h_transfer_encoding:
case h_upgrade:
if (ch != ' ')
if (ch != ' ') {
parser->header_state = h_general;
}
break;
default:
@ -1539,8 +1548,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
}
case s_header_value_discard_ws:
if (ch == ' ' || ch == '\t')
if (ch == ' ' || ch == '\t') {
break;
}
if (ch == CR) {
UPDATE_STATE(s_header_value_discard_ws_almost_done);
@ -1644,10 +1654,11 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
p_cr = memchr(p, CR, limit);
p_lf = memchr(p, LF, limit);
if (p_cr != NULL) {
if (p_lf != NULL && p_cr >= p_lf)
if (p_lf != NULL && p_cr >= p_lf) {
p = p_lf;
else
} else {
p = p_cr;
}
} else if (UNLIKELY(p_lf != NULL)) {
p = p_lf;
} else {
@ -1666,8 +1677,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
case h_content_length: {
uint64_t t;
if (ch == ' ')
if (ch == ' ') {
break;
}
if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
@ -1754,8 +1766,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
break;
case h_transfer_encoding_chunked:
if (ch != ' ')
if (ch != ' ') {
h_state = h_general;
}
break;
case h_connection_keep_alive:
@ -1786,8 +1799,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett
COUNT_HEADER_SIZE(p - start);
if (p == data + len)
if (p == data + len) {
--p;
}
break;
}

View File

@ -21,9 +21,11 @@ static const char response_404[] = "HTTP/1.1 404 Not Found" CRLF "Server: Haywir
static kh_inline khint_t hw_string_hash_func(hw_string *s) {
khint_t h = s->length > 0 ? (khint_t)*s->value : 0;
if (h)
for (int i = 0; i < s->length; i++)
if (h) {
for (int i = 0; i < s->length; i++) {
h = (h << 5) - h + (khint_t) * (s->value + i);
}
}
return h;
}

View File

@ -54,8 +54,9 @@ void hw_set_body(hw_http_response *response, hw_string *body) {
int num_chars(int n) {
int r = 1;
if (n < 0)
if (n < 0) {
n = (n == INT_MIN) ? INT_MAX : -n;
}
while (n > 9) {
n /= 10;
r++;

View File

@ -413,9 +413,11 @@ static const double __ac_HASH_UPPER = 0.77;
*/
static kh_inline khint_t __ac_X31_hash_string(const char *s) {
khint_t h = (khint_t)*s;
if (h)
for (++s; *s; ++s)
if (h) {
for (++s; *s; ++s) {
h = (h << 5) - h + (khint_t)*s;
}
}
return h;
}
/*! @function

View File

@ -141,8 +141,9 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const
;
int found;
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
if (found)
if (found) {
goto FOUND_CTL;
}
#else
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
while (likely(buf_end - buf >= 8)) {
@ -483,8 +484,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
case CHUNKED_IN_CHUNK_SIZE:
for (;; ++src) {
int v;
if (src == bufsz)
if (src == bufsz) {
goto Exit;
}
if ((v = decode_hex(buf[src])) == -1) {
if (decoder->_hex_count == 0) {
ret = -1;
@ -505,11 +507,13 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
case CHUNKED_IN_CHUNK_EXT:
/* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
for (;; ++src) {
if (src == bufsz)
if (src == bufsz) {
goto Exit;
if (buf[src] == '\012')
}
if (buf[src] == '\012') {
break;
}
}
++src;
if (decoder->bytes_left_in_chunk == 0) {
if (decoder->consume_trailer) {
@ -524,15 +528,17 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
case CHUNKED_IN_CHUNK_DATA: {
size_t avail = bufsz - src;
if (avail < decoder->bytes_left_in_chunk) {
if (dst != src)
if (dst != src) {
memmove(buf + dst, buf + src, avail);
}
src += avail;
dst += avail;
decoder->bytes_left_in_chunk -= avail;
goto Exit;
}
if (dst != src)
if (dst != src) {
memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
}
src += decoder->bytes_left_in_chunk;
dst += decoder->bytes_left_in_chunk;
decoder->bytes_left_in_chunk = 0;
@ -541,11 +547,13 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
/* fallthru */
case CHUNKED_IN_CHUNK_CRLF:
for (;; ++src) {
if (src == bufsz)
if (src == bufsz) {
goto Exit;
if (buf[src] != '\015')
}
if (buf[src] != '\015') {
break;
}
}
if (buf[src] != '\012') {
ret = -1;
goto Exit;
@ -555,22 +563,27 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
break;
case CHUNKED_IN_TRAILERS_LINE_HEAD:
for (;; ++src) {
if (src == bufsz)
if (src == bufsz) {
goto Exit;
if (buf[src] != '\015')
}
if (buf[src] != '\015') {
break;
}
if (buf[src++] == '\012')
}
if (buf[src++] == '\012') {
goto Complete;
}
decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
/* fallthru */
case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
for (;; ++src) {
if (src == bufsz)
if (src == bufsz) {
goto Exit;
if (buf[src] == '\012')
}
if (buf[src] == '\012') {
break;
}
}
++src;
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
break;
@ -582,8 +595,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
Complete:
ret = bufsz - src;
Exit:
if (dst != src)
if (dst != src) {
memmove(buf + dst, buf + src, bufsz - src);
}
*_bufsz = dst;
return ret;
}

View File

@ -125,7 +125,6 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
void user_uninit() {
task_manager_exit();
free_http_server();
uv_sleep(1000);
mq_uninit();
zlog_fini();
uv_loop_close(get_task_manager());

File diff suppressed because it is too large Load Diff

View File

@ -74,17 +74,14 @@ struct data7 //cache
{
char *mapname;
time_t expiry;
union
{
struct
{
union {
struct {
MYBYTE cType;
MYBYTE dnsType;
MYBYTE sockInd;
MYBYTE dnsIndex;
};
struct
{
struct {
unsigned fixed : 1;
unsigned local : 1;
unsigned display : 1;
@ -93,19 +90,16 @@ struct data7 //cache
unsigned dhcpInd : 20;
};
};
union
{
union {
char *name;
MYBYTE *options;
};
union
{
union {
int bytes;
MYDWORD ip;
SOCKADDR_IN *addr;
};
union
{
union {
MYBYTE *response;
char *hostname;
char *query;
@ -131,8 +125,7 @@ typedef multimap<string, data7*> hostMap;
typedef multimap<time_t, data7 *> expiryMap;
typedef map<string, data7 *> dhcpMap;
struct ConnType
{
struct ConnType {
SOCKET sock;
SOCKADDR_IN addr;
MYDWORD server;
@ -154,7 +147,6 @@ struct ConnType
#define DHCP_MESS_RELEASE 7
#define DHCP_MESS_INFORM 8
// DHCP OPTIONS
#define DHCP_OPTION_PAD 0
#define DHCP_OPTION_NETMASK 1
@ -287,30 +279,27 @@ struct ConnType
#define VM_STANFORD 0x5354414EUL
#define VM_RFC1048 0x63825363UL
struct data3
{
struct data3 {
MYBYTE opt_code;
MYBYTE size;
MYBYTE value[256];
};
struct data4
{
struct data4 {
char opName[40];
MYBYTE opTag;
MYBYTE opType;
bool permitted;
};
#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) \
|| defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
#define MY_MAX_TIME UINT_MAX
struct data8//client
{
union
{
union {
MYDWORD filler;
struct
{
struct {
unsigned dhcpInd : 20;
unsigned bp_hlen : 8;
unsigned fixed : 1;
@ -328,11 +317,9 @@ struct data8 //client
#define MY_MAX_TIME INT_MAX
struct data8//client
{
union
{
union {
MYDWORD filler;
struct
{
struct {
unsigned dhcpInd : 20;
unsigned bp_hlen : 8;
unsigned fixed : 1;
@ -349,16 +336,14 @@ struct data8 //client
};
#endif
struct msg_control
{
struct msg_control {
ulong cmsg_len;
int cmsg_level;
int cmsg_type;
in_pktinfo pktinfo;
};
struct dhcp_header
{
struct dhcp_header {
MYBYTE bp_op;
MYBYTE bp_htype;
MYBYTE bp_hlen;
@ -377,8 +362,7 @@ struct dhcp_header
MYBYTE bp_magic_num[4];
};
struct dhcp_packet
{
struct dhcp_packet {
dhcp_header header;
MYBYTE vend_data[1024 - sizeof(dhcp_header)];
};
@ -408,18 +392,15 @@ struct data14 //rangeSet
MYDWORD targetIP;
};
struct data15
{
union
{
struct data15 {
union {
//MYDWORD ip;
unsigned ip : 32;
MYBYTE octate[4];
};
};
struct data17
{
struct data17 {
MYBYTE macArray[MAX_RANGE_SETS];
MYBYTE vendArray[MAX_RANGE_SETS];
MYBYTE userArray[MAX_RANGE_SETS];
@ -430,8 +411,7 @@ struct data17
bool subnetFound;
};
struct data19
{
struct data19 {
SOCKET sock;
socklen_t sockLen;
SOCKADDR_IN remote;
@ -441,8 +421,7 @@ struct data19
char *dp;
};
struct data20
{
struct data20 {
MYBYTE options[sizeof(dhcp_packet)];
MYWORD optionSize;
MYDWORD ip;
@ -453,8 +432,7 @@ struct data20
struct data9//dhcpRequst
{
MYDWORD lease;
union
{
union {
char raw[sizeof(dhcp_packet)];
dhcp_packet dhcpp;
};
@ -486,8 +464,7 @@ struct data9 //dhcpRequst
MYBYTE sockInd;
};
struct DhcpConnType
{
struct DhcpConnType {
SOCKET sock;
SOCKADDR_IN addr;
MYDWORD server;
@ -497,20 +474,17 @@ struct DhcpConnType
MYDWORD mask;
int reUseVal;
int reUseSize;
union
{
union {
int broadCastVal;
bool pktinfoVal;
};
union
{
union {
int broadCastSize;
unsigned int pktinfoSize;
};
};
struct data1
{
struct data1 {
DhcpConnType dhcpConn[MAX_SERVERS];
DhcpConnType dhcpListener;
ConnType httpConn;
@ -525,8 +499,7 @@ struct data1
bool busy;
};
struct data2
{
struct data2 {
char servername[128];
char servername_fqn[256];
char zone[256];
@ -561,7 +534,6 @@ struct data2
char rangeCount;
};
//Function Prototypes
bool checkIP(data9 *req, data17 *rangeData, MYDWORD ip);
bool checkMask(MYDWORD);