1 /* packet-http-urlencoded.c
2 * Routines for dissection of HTTP urlecncoded form, based on packet-text-media.c (C) Olivier Biot, 2004.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #define NEW_PROTO_TREE_API
31 #include <epan/packet.h>
32 #include <epan/wmem/wmem.h>
34 static dissector_handle_t form_urlencoded_handle
;
36 static header_field_info
*hfi_urlencoded
= NULL
;
38 #define URLENCODED_HFI_INIT HFI_INIT(proto_urlencoded)
40 static header_field_info hfi_form_keyvalue URLENCODED_HFI_INIT
=
41 { "Form item", "urlencoded-form", FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
};
43 static header_field_info hfi_form_key URLENCODED_HFI_INIT
=
44 { "Key", "urlencoded-form.key", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
};
46 static header_field_info hfi_form_value URLENCODED_HFI_INIT
=
47 { "Value", "urlencoded-form.value", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
};
49 static gint ett_form_urlencoded
= -1;
50 static gint ett_form_keyvalue
= -1;
55 if (a
>= '0' && a
<= '9')
57 if (a
>= 'A' && a
<= 'F')
58 return (a
- 'A') + 10;
59 if (a
>= 'a' && a
<= 'f')
60 return (a
- 'a') + 10;
66 get_form_key_value(tvbuff_t
*tvb
, char **ptr
, int offset
, char stop
)
68 const int orig_offset
= offset
;
73 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
76 ch
= tvb_get_guint8(tvb
, offset
);
83 ch
= tvb_get_guint8(tvb
, offset
);
84 if (get_hexa(ch
) > 15)
88 ch
= tvb_get_guint8(tvb
, offset
);
89 if (get_hexa(ch
) > 15)
97 *ptr
= tmp
= (char*)wmem_alloc(wmem_packet_scope(), len
+ 1);
101 offset
= orig_offset
;
102 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
105 ch
= tvb_get_guint8(tvb
, offset
);
115 ch1
= tvb_get_guint8(tvb
, offset
);
118 ch2
= tvb_get_guint8(tvb
, offset
);
120 tmp
[len
] = get_hexa(ch1
) << 4 | get_hexa(ch2
);
122 } else if (ch
== '+')
136 dissect_form_urlencoded(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data
)
138 proto_tree
*url_tree
;
141 gint offset
= 0, next_offset
;
142 const char *data_name
;
144 data_name
= pinfo
->match_string
;
145 if (! (data_name
&& data_name
[0])) {
147 * No information from "match_string"
149 data_name
= (char *)data
;
150 if (! (data_name
&& data_name
[0])) {
152 * No information from dissector data
154 data_name
= (char *)(pinfo
->private_data
);
155 if (! (data_name
&& data_name
[0])) {
157 * No information from "private_data"
165 col_append_sep_fstr(pinfo
->cinfo
, COL_INFO
, " ", "(%s)", data_name
);
167 ti
= proto_tree_add_item(tree
, hfi_urlencoded
, tvb
, 0, -1, ENC_NA
);
169 proto_item_append_text(ti
, ": %s", data_name
);
170 url_tree
= proto_item_add_subtree(ti
, ett_form_urlencoded
);
172 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
173 const int start_offset
= offset
;
176 ti
= proto_tree_add_item(url_tree
, &hfi_form_keyvalue
, tvb
, offset
, 0, ENC_NA
);
178 sub
= proto_item_add_subtree(ti
, ett_form_keyvalue
);
180 next_offset
= get_form_key_value(tvb
, &key
, offset
, '=');
181 if (next_offset
== -1)
183 proto_tree_add_string(sub
, &hfi_form_key
, tvb
, offset
, next_offset
- offset
, key
);
184 proto_item_append_text(sub
, ": \"%s\"", key
);
186 offset
= next_offset
+1;
188 next_offset
= get_form_key_value(tvb
, &value
, offset
, '&');
189 if (next_offset
== -1)
191 proto_tree_add_string(sub
, &hfi_form_value
, tvb
, offset
, next_offset
- offset
, value
);
192 proto_item_append_text(sub
, " = \"%s\"", value
);
194 offset
= next_offset
+1;
196 proto_item_set_len(ti
, offset
- start_offset
);
199 return tvb_length(tvb
);
203 proto_register_http_urlencoded(void)
205 #ifndef HAVE_HFI_SECTION_INIT
206 static header_field_info
*hfi
[] = {
213 static gint
*ett
[] = {
214 &ett_form_urlencoded
,
218 int proto_urlencoded
;
220 proto_urlencoded
= proto_register_protocol("HTML Form URL Encoded", "URL Encoded Form Data", "urlencoded-form");
221 hfi_urlencoded
= proto_registrar_get_nth(proto_urlencoded
);
223 form_urlencoded_handle
= new_register_dissector("urlencoded-form", dissect_form_urlencoded
, proto_urlencoded
);
225 proto_register_fields(proto_urlencoded
, hfi
, array_length(hfi
));
226 proto_register_subtree_array(ett
, array_length(ett
));
230 proto_reg_handoff_http_urlencoded(void)
232 dissector_add_string("media_type", "application/x-www-form-urlencoded", form_urlencoded_handle
);
236 * Editor modelines - http://www.wireshark.org/tools/modelines.html
241 * indent-tabs-mode: t
244 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
245 * :indentSize=8:tabSize=8:noTabs=false: