json loggers: dup bstr with bstr_util_strdup_to_c

In various places SCStrndup was used to 'dup' a bstr string, however
libhtp provides bstr_util_strdup_to_c for this. As this is a cleaner
interface, it's preferred.
pull/809/merge
Victor Julien 13 years ago
parent 0032ad34d4
commit 2b60871bf1

@ -81,11 +81,11 @@ static json_t *LogFileMetaGetUri(const Packet *p, const File *ff) {
if (tx != NULL) { if (tx != NULL) {
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx); HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
if (tx_ud->request_uri_normalized != NULL) { if (tx_ud->request_uri_normalized != NULL) {
char *s = SCStrndup((char *) bstr_ptr(tx_ud->request_uri_normalized), char *s = bstr_util_strdup_to_c(tx_ud->request_uri_normalized);
bstr_len(tx_ud->request_uri_normalized)); if (s != NULL) {
js = json_string(s); js = json_string(s);
if (s != NULL)
SCFree(s); SCFree(s);
}
} }
return js; return js;
} }
@ -100,11 +100,11 @@ static json_t *LogFileMetaGetHost(const Packet *p, const File *ff) {
if (htp_state != NULL) { if (htp_state != NULL) {
htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP, htp_state, ff->txid); htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP, htp_state, ff->txid);
if (tx != NULL && tx->request_hostname != NULL) { if (tx != NULL && tx->request_hostname != NULL) {
char *s = SCStrndup((char *) bstr_ptr(tx->request_hostname), char *s = bstr_util_strdup_to_c(tx->request_hostname);
bstr_len(tx->request_hostname)); if (s != NULL) {
js = json_string(s); js = json_string(s);
if (s != NULL)
SCFree(s); SCFree(s);
}
return js; return js;
} }
} }
@ -122,11 +122,11 @@ static json_t *LogFileMetaGetReferer(const Packet *p, const File *ff) {
h = (htp_header_t *)htp_table_get_c(tx->request_headers, h = (htp_header_t *)htp_table_get_c(tx->request_headers,
"Referer"); "Referer");
if (h != NULL) { if (h != NULL) {
char *s = SCStrndup((char *)bstr_ptr(h->value), char *s = bstr_util_strdup_to_c(h->value);
bstr_len(h->value)); if (s != NULL) {
js = json_string(s); js = json_string(s);
if (s != NULL)
SCFree(s); SCFree(s);
}
return js; return js;
} }
} }
@ -145,11 +145,11 @@ static json_t *LogFileMetaGetUserAgent(const Packet *p, const File *ff) {
h = (htp_header_t *)htp_table_get_c(tx->request_headers, h = (htp_header_t *)htp_table_get_c(tx->request_headers,
"User-Agent"); "User-Agent");
if (h != NULL) { if (h != NULL) {
char *s = SCStrndup((char *)bstr_ptr(h->value), char *s = bstr_util_strdup_to_c(h->value);
bstr_len(h->value)); if (s != NULL) {
js = json_string(s); js = json_string(s);
if (s != NULL)
SCFree(s); SCFree(s);
}
return js; return js;
} }
} }

@ -83,8 +83,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
/* hostname */ /* hostname */
if (tx->request_hostname != NULL) if (tx->request_hostname != NULL)
{ {
c = SCStrndup((char *)bstr_ptr(tx->request_hostname), c = bstr_util_strdup_to_c(tx->request_hostname);
bstr_len(tx->request_hostname));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "hostname", json_string(c)); json_object_set_new(hjs, "hostname", json_string(c));
SCFree(c); SCFree(c);
@ -96,8 +95,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
/* uri */ /* uri */
if (tx->request_uri != NULL) if (tx->request_uri != NULL)
{ {
c = SCStrndup((char *)bstr_ptr(tx->request_uri), c = bstr_util_strdup_to_c(tx->request_uri);
bstr_len(tx->request_uri));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "url", json_string(c)); json_object_set_new(hjs, "url", json_string(c));
SCFree(c); SCFree(c);
@ -110,8 +108,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
h_user_agent = htp_table_get_c(tx->request_headers, "user-agent"); h_user_agent = htp_table_get_c(tx->request_headers, "user-agent");
} }
if (h_user_agent != NULL) { if (h_user_agent != NULL) {
c = SCStrndup((char *)bstr_ptr(h_user_agent->value), c = bstr_util_strdup_to_c(h_user_agent->value);
bstr_len(h_user_agent->value));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "http_user_agent", json_string(c)); json_object_set_new(hjs, "http_user_agent", json_string(c));
SCFree(c); SCFree(c);
@ -126,8 +123,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
h_x_forwarded_for = htp_table_get_c(tx->request_headers, "x-forwarded-for"); h_x_forwarded_for = htp_table_get_c(tx->request_headers, "x-forwarded-for");
} }
if (h_x_forwarded_for != NULL) { if (h_x_forwarded_for != NULL) {
c = SCStrndup((char *)bstr_ptr(h_x_forwarded_for->value), c = bstr_util_strdup_to_c(h_x_forwarded_for->value);
bstr_len(h_x_forwarded_for->value));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "xff", json_string(c)); json_object_set_new(hjs, "xff", json_string(c));
SCFree(c); SCFree(c);
@ -141,8 +137,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
} }
if (h_content_type != NULL) { if (h_content_type != NULL) {
char *p; char *p;
c = SCStrndup((char *)bstr_ptr(h_content_type->value), c = bstr_util_strdup_to_c(h_content_type->value);
bstr_len(h_content_type->value));
if (c != NULL) { if (c != NULL) {
p = strchrnul(c, ';'); p = strchrnul(c, ';');
*p = '\0'; *p = '\0';
@ -158,8 +153,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
h_referer = htp_table_get_c(tx->request_headers, "referer"); h_referer = htp_table_get_c(tx->request_headers, "referer");
} }
if (h_referer != NULL) { if (h_referer != NULL) {
c = SCStrndup((char *)bstr_ptr(h_referer->value), c = bstr_util_strdup_to_c(h_referer->value);
bstr_len(h_referer->value));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "http_refer", json_string(c)); json_object_set_new(hjs, "http_refer", json_string(c));
SCFree(c); SCFree(c);
@ -168,8 +162,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
/* method */ /* method */
if (tx->request_method != NULL) { if (tx->request_method != NULL) {
c = SCStrndup((char *)bstr_ptr(tx->request_method), c = bstr_util_strdup_to_c(tx->request_method);
bstr_len(tx->request_method));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "http_method", json_string(c)); json_object_set_new(hjs, "http_method", json_string(c));
SCFree(c); SCFree(c);
@ -178,8 +171,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
/* protocol */ /* protocol */
if (tx->request_protocol != NULL) { if (tx->request_protocol != NULL) {
c = SCStrndup((char *)bstr_ptr(tx->request_protocol), c = bstr_util_strdup_to_c(tx->request_protocol);
bstr_len(tx->request_protocol));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "protocol", json_string(c)); json_object_set_new(hjs, "protocol", json_string(c));
SCFree(c); SCFree(c);
@ -188,8 +180,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
/* response status */ /* response status */
if (tx->response_status != NULL) { if (tx->response_status != NULL) {
c = SCStrndup((char *)bstr_ptr(tx->response_status), c = bstr_util_strdup_to_c(tx->response_status);
bstr_len(tx->response_status));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "status", json_string(c)); json_object_set_new(hjs, "status", json_string(c));
SCFree(c); SCFree(c);
@ -197,8 +188,7 @@ static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx)
htp_header_t *h_location = htp_table_get_c(tx->response_headers, "location"); htp_header_t *h_location = htp_table_get_c(tx->response_headers, "location");
if (h_location != NULL) { if (h_location != NULL) {
c = SCStrndup((char *)bstr_ptr(h_location->value), c = bstr_util_strdup_to_c(h_location->value);
bstr_len(h_location->value));
if (c != NULL) { if (c != NULL) {
json_object_set_new(hjs, "redirect", json_string(c)); json_object_set_new(hjs, "redirect", json_string(c));
SCFree(c); SCFree(c);

Loading…
Cancel
Save