Skip to content

Commit 9067ec9

Browse files
author
Daniel Herzog
committed
Using the http-header tokenizer from network-templates
1 parent b3c17b0 commit 9067ec9

File tree

4 files changed

+109
-19
lines changed

4 files changed

+109
-19
lines changed

src/network/network_details_templates.js

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,70 @@ templates._response = function(response, is_last, do_raw)
123123
]
124124
};
125125

126+
templates._make_header_template_func = function(is_request_headers)
127+
{
128+
// add data-spec attributes on certain firstline tokens, depending on if it's request_headers.
129+
// todo: while this has firstline_tokens, it can't be reused.
130+
var firstline_tokens = 0;
131+
var add_data_spec;
132+
if (is_request_headers)
133+
{
134+
add_data_spec = {
135+
0: true
136+
};
137+
}
138+
else
139+
{
140+
add_data_spec = {
141+
1: true
142+
};
143+
}
144+
145+
return function(token)
146+
{
147+
var TYPE = 0;
148+
var STR = 1;
149+
var attrs = ["class", "header-token-type-" + cls.HTTPHeaderTokenizer.classnames[token[TYPE]]];
150+
if (token[TYPE] === cls.HTTPHeaderTokenizer.types.FIRST_LINE_PART)
151+
{
152+
if (firstline_tokens in add_data_spec)
153+
{
154+
attrs.extend(["data-spec", "http#" + token[STR]])
155+
}
156+
firstline_tokens++;
157+
}
158+
return ["span", token[STR]].concat(attrs);
159+
}
160+
}
161+
162+
templates._token_receiver = function(tokens, token_type, token)
163+
{
164+
tokens.push([token_type, token]);
165+
};
166+
126167
templates._request_headers = function(req, do_raw)
127168
{
128169
if (do_raw)
129170
{
130-
return [
131-
["h2", ui_strings.S_NETWORK_REQUEST_DETAIL_REQUEST_TITLE],
132-
["pre", req.request_headers_raw, "class", "mono"]
133-
];
171+
if (req.request_headers_raw) // todo: we explicitely mention missing request headers in parsed. this check here is a bit ugly.
172+
{
173+
if (!req.header_tokens)
174+
{
175+
var tokens = [];
176+
var tokenizer = new cls.HTTPHeaderTokenizer();
177+
tokenizer.tokenize(req.request_headers_raw, templates._token_receiver.bind(this, tokens));
178+
req.header_tokens = tokens;
179+
}
180+
if (req.header_tokens.length)
181+
{
182+
var map_func = templates._make_header_template_func(true);
183+
return [
184+
["h2", ui_strings.S_NETWORK_REQUEST_DETAIL_REQUEST_TITLE],
185+
["pre", req.header_tokens.map(map_func), "class", "mono"]
186+
];
187+
}
188+
}
189+
return [];
134190
}
135191

136192
var ret = [];
@@ -166,16 +222,28 @@ templates._request_headers = function(req, do_raw)
166222

167223
templates._response_headers = function(resp, do_raw)
168224
{
225+
if (!resp.response_headers) // todo: we explicitely mention missing request headers but not missing response headers // ui_strings.S_NETWORK_REQUEST_NO_HEADERS_LABEL
226+
return [];
227+
169228
if (do_raw)
170229
{
171-
return [
172-
["h2", ui_strings.S_NETWORK_REQUEST_DETAIL_RESPONSE_TITLE],
173-
["pre", resp.response_headers_raw, "class", "mono"]
174-
];
175-
}
176-
177-
if (!resp.response_headers) // todo: we explicitely mention missing request headers but not missing response headers // ui_strings.S_NETWORK_REQUEST_NO_HEADERS_LABEL
230+
if (!resp.header_tokens)
231+
{
232+
var tokens = [];
233+
var tokenizer = new cls.HTTPHeaderTokenizer();
234+
tokenizer.tokenize(resp.response_headers_raw, templates._token_receiver.bind(this, tokens));
235+
resp.header_tokens = tokens;
236+
}
237+
if (resp.header_tokens.length)
238+
{
239+
var map_func = templates._make_header_template_func(false);
240+
return [
241+
["h2", ui_strings.S_NETWORK_REQUEST_DETAIL_RESPONSE_TITLE],
242+
["pre", resp.header_tokens.map(map_func), "class", "mono"]
243+
];
244+
}
178245
return [];
246+
}
179247

180248
var ret = [];
181249

src/network/network_service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ cls.NetworkLoggerRequest = function(entry)
919919
this.firstline = null;
920920
this.requestbody = null;
921921
this.boundary = "";
922+
this.header_tokens = null; // This is set from template code, when it's first needed.
922923
};
923924

924925
cls.NetworkLoggerRequestPrototype = function()
@@ -971,6 +972,7 @@ cls.NetworkLoggerResponse = function(entry)
971972
this.response_headers_raw = null;
972973
this.firstline = null;
973974
this.responsebody = null;
975+
this.header_tokens = null; // This is set from template code, when it's first needed.
974976

975977
// The following are duplicated from the entry to have them available directly on the response
976978
this.logger_entry_type = entry.type;

src/network/network_style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@
277277
-1px 0 0 rgba(0, 0, 0, 0.05);
278278
}
279279

280+
.network_logger .header-token-type-first_line_part
281+
{
282+
font-weight: bold;
283+
}
284+
285+
.network_logger .header-token-type-name
286+
{
287+
color: #006C0E;
288+
}
289+
280290
.network_logger .network-summary
281291
{
282292
clear: both;

src/syntaxhighlight/http-header/tokenizer.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ cls.HTTPHeaderTokenizer = function()
1212

1313
cls.HTTPHeaderTokenizerPrototype = function()
1414
{
15-
var LF = "\n";
1615
var PUNCTUATOR = ":";
1716
var WHITESPACE_CHARS = {
1817
"\u0009": 1, // Tab <TAB>
1918
"\u0020": 1 // Space <SP>
2019
};
20+
var LINEBREAK_CHARS = {
21+
"\r": 1, // CR
22+
"\n": 1 // LF
23+
};
2124

2225
this.tokenize = function(input_buffer, ontoken)
2326
{
@@ -45,7 +48,7 @@ cls.HTTPHeaderTokenizerPrototype = function()
4548
this._token_buffer = "";
4649
}
4750
else
48-
if (c === LF)
51+
if (c in LINEBREAK_CHARS)
4952
{
5053
this._emitToken(this._token_type ,this._token_buffer);
5154
this._token_buffer = "";
@@ -77,8 +80,8 @@ cls.HTTPHeaderTokenizerPrototype = function()
7780

7881
var c = this._buffer.charAt(this._current_pos++);
7982
this._token_type = cls.HTTPHeaderTokenizer.types.VALUE;
80-
// LF only means switching to header when the following char is not whitespace.
81-
if (c === LF && !(this._buffer.charAt(this._current_pos) in WHITESPACE_CHARS))
83+
// LINEBREAK_CHARS only mean switching to header when the following char is not whitespace.
84+
if (c in LINEBREAK_CHARS && !(this._buffer.charAt(this._current_pos) in WHITESPACE_CHARS))
8285
{
8386
this._emitToken(this._token_type ,this._token_buffer);
8487
this._token_buffer = "";
@@ -106,8 +109,15 @@ cls.HTTPHeaderTokenizerPrototype = function()
106109
cls.HTTPHeaderTokenizer.prototype = new cls.HTTPHeaderTokenizerPrototype();
107110

108111
cls.HTTPHeaderTokenizer.types = {
109-
FIRST_LINE_PART : 1,
110-
NAME : 2,
111-
VALUE : 3,
112-
PUNCTUATOR : 4
112+
FIRST_LINE_PART : 1,
113+
NAME : 2,
114+
VALUE : 3,
115+
PUNCTUATOR : 4
116+
};
117+
118+
cls.HTTPHeaderTokenizer.classnames = {
119+
1 : "first_line_part",
120+
2 : "name" ,
121+
3 : "value" ,
122+
4 : "punctuator"
113123
};

0 commit comments

Comments
 (0)