-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathparsers.js
More file actions
118 lines (112 loc) · 2.91 KB
/
parsers.js
File metadata and controls
118 lines (112 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* www-authenticate
* https://github.com/randymized/www-authenticate
*
* Copyright (c) 2013 Randy McLaughlin
* Licensed under the MIT license.
*/
/*
* Copyright © 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var ParseAuth= /(\w+)\s+(.*)/ // -> scheme, params
, Separators= /([",=])/
;
function parse_params(header) {
// This parser will definitely fail if there is more than one challenge
var tok, _i, _len, key, value;
var state= 0; //0: token,
var m= header.split(Separators)
for (_i = 0, _len = m.length; _i < _len; _i++) {
tok = m[_i];
if (!tok.length) continue;
switch (state) {
case 0: // token
key= tok.trim();
state= 1; // expect equals
continue;
case 1: // expect equals
if ('=' != tok) return 'Equal sign was expected after '+key;
state= 2;
continue;
case 2: // expect value
if ('"' == tok) {
value= '';
state= 3; // expect quoted
continue;
}
else {
this.parms[key]= value= tok.trim();
state= 9; // expect comma or end
continue;
}
case 3: // handling quoted string
if ('"' == tok) {
state= 8; // end quoted
continue;
}
else {
value+= tok;
state= 3; // continue accumulating quoted string
continue;
}
case 8: // end quote encountered
if ('"' == tok) {
// double quoted
value+= '"';
state= 3; // back to quoted string
continue;
}
if (',' == tok) {
this.parms[key]= value;
state= 0;
continue;
}
else {
return 'Unexpected token ('+tok+') after '+value+'"';
}
case 9: // expect commma
if (',' != tok) return 'Comma expected after '+value;
state= 0;
continue;
}
}
switch (state) { // terminal state
case 0: // Empty or ignoring terminal comma
case 9: // Expecting comma or end of header
return;
case 8: // Last token was end quote
this.parms[key]= value;
return;
default:
return 'Unexpected end of www-authenticate value.';
}
}
function Parse_WWW_Authenticate(to_parse)
{
var m= to_parse.match(ParseAuth);
this.scheme= m[1];
this.parms= {};
var err= this.parse_params(m[2]);
if (err) {
this.scheme= '';
this.parms= {};
this.err= err;
}
}
function Parse_Authentication_Info(to_parse)
{
this.scheme= 'Digest';
this.parms= {};
var err= this.parse_params(to_parse);
if (err) {
this.scheme= '';
this.parms= {};
this.err= err;
}
}
Parse_Authentication_Info.prototype.parse_params= parse_params;
Parse_WWW_Authenticate.prototype.parse_params= parse_params;
module.exports = {
WWW_Authenticate: Parse_WWW_Authenticate,
Authentication_Info: Parse_Authentication_Info
};