1 /* packet-http-urlencoded.c
2 * Routines for dissection of HTTP urlecncoded form, based on packet-text-media.c (C) Olivier Biot, 2004.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #define NEW_PROTO_TREE_API
15 #include <epan/packet.h>
16 #include <wsutil/str_util.h>
18 #include "packet-http.h"
20 void proto_register_http_urlencoded(void);
21 void proto_reg_handoff_http_urlencoded(void);
23 static dissector_handle_t form_urlencoded_handle;
25 static header_field_info *hfi_urlencoded = NULL;
27 #define URLENCODED_HFI_INIT HFI_INIT(proto_urlencoded)
29 static header_field_info hfi_form_key URLENCODED_HFI_INIT =
30 { "Key", "urlencoded-form.key", FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL };
32 static header_field_info hfi_form_value URLENCODED_HFI_INIT =
33 { "Value", "urlencoded-form.value", FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL };
35 static gint ett_form_urlencoded = -1;
36 static gint ett_form_keyvalue = -1;
39 get_form_key_value(tvbuff_t *tvb, char **ptr, int offset, char stop)
41 const int orig_offset = offset;
46 while (tvb_reported_length_remaining(tvb, offset) > 0) {
49 ch = tvb_get_guint8(tvb, offset);
56 ch = tvb_get_guint8(tvb, offset);
57 if (ws_xton(ch) == -1)
61 ch = tvb_get_guint8(tvb, offset);
62 if (ws_xton(ch) == -1)
70 *ptr = tmp = (char*)wmem_alloc(wmem_packet_scope(), len + 1);
75 while (tvb_reported_length_remaining(tvb, offset) > 0) {
78 ch = tvb_get_guint8(tvb, offset);
88 ch1 = tvb_get_guint8(tvb, offset);
91 ch2 = tvb_get_guint8(tvb, offset);
93 tmp[len] = ws_xton(ch1) << 4 | ws_xton(ch2);
109 dissect_form_urlencoded(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
111 proto_tree *url_tree;
114 gint offset = 0, next_offset;
115 const char *data_name;
116 http_message_info_t *message_info;
118 data_name = pinfo->match_string;
119 if (! (data_name && data_name[0])) {
121 * No information from "match_string"
123 message_info = (http_message_info_t *)data;
124 if (message_info == NULL) {
126 * No information from dissector data
130 data_name = message_info->media_str;
131 if (! (data_name && data_name[0])) {
133 * No information from dissector data
141 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(%s)", data_name);
143 ti = proto_tree_add_item(tree, hfi_urlencoded, tvb, 0, -1, ENC_NA);
145 proto_item_append_text(ti, ": %s", data_name);
146 url_tree = proto_item_add_subtree(ti, ett_form_urlencoded);
148 while (tvb_reported_length_remaining(tvb, offset) > 0) {
149 const int start_offset = offset;
152 sub = proto_tree_add_subtree(url_tree, tvb, offset, 0, ett_form_keyvalue, &ti, "Form item");
154 next_offset = get_form_key_value(tvb, &key, offset, '=');
155 if (next_offset == -1)
157 proto_tree_add_string(sub, &hfi_form_key, tvb, offset, next_offset - offset, key);
158 proto_item_append_text(sub, ": \"%s\"", key);
160 offset = next_offset+1;
162 next_offset = get_form_key_value(tvb, &value, offset, '&');
163 if (next_offset == -1)
165 proto_tree_add_string(sub, &hfi_form_value, tvb, offset, next_offset - offset, value);
166 proto_item_append_text(sub, " = \"%s\"", value);
168 offset = next_offset+1;
170 proto_item_set_len(ti, offset - start_offset);
173 return tvb_captured_length(tvb);
177 proto_register_http_urlencoded(void)
179 #ifndef HAVE_HFI_SECTION_INIT
180 static header_field_info *hfi[] = {
186 static gint *ett[] = {
187 &ett_form_urlencoded,
191 int proto_urlencoded;
193 proto_urlencoded = proto_register_protocol("HTML Form URL Encoded", "URL Encoded Form Data", "urlencoded-form");
194 hfi_urlencoded = proto_registrar_get_nth(proto_urlencoded);
196 form_urlencoded_handle = register_dissector("urlencoded-form", dissect_form_urlencoded, proto_urlencoded);
198 proto_register_fields(proto_urlencoded, hfi, array_length(hfi));
199 proto_register_subtree_array(ett, array_length(ett));
203 proto_reg_handoff_http_urlencoded(void)
205 dissector_add_string("media_type", "application/x-www-form-urlencoded", form_urlencoded_handle);
209 * Editor modelines - http://www.wireshark.org/tools/modelines.html
214 * indent-tabs-mode: t
217 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
218 * :indentSize=8:tabSize=8:noTabs=false: