MCPcopy Create free account
hub / github.com/cloudflare/wildebeest / parseRequest

Function parseRequest

backend/src/utils/httpsigjs/parser.ts:112–378  ·  view source on GitHub ↗
(request: Request, options?: Options)

Source from the content-addressed store, hash-verified

110 * @throws {ExpiredRequestError} if the value of date or x-date exceeds skew.
111 */
112export function parseRequest(request: Request, options?: Options): ParsedSignature {
113 if (options === undefined) {
114 options = {
115 clockSkew: 300,
116 headers: ['host', '(request-target)'],
117 strict: false,
118 }
119 }
120
121 if (request.method == 'POST') {
122 options.headers.push('digest')
123 }
124
125 let headers = [request.headers.has('x-date') ? 'x-date' : 'date']
126 if (options.headers !== undefined) {
127 headers = options.headers
128 }
129
130 const authz = request.headers.get(HEADER.AUTH) || request.headers.get(HEADER.SIG)
131
132 if (!authz) {
133 const errHeader = HEADER.AUTH + ' or ' + HEADER.SIG
134
135 throw new MissingHeaderError('no ' + errHeader + ' header ' + 'present in the request')
136 }
137
138 options.clockSkew = options.clockSkew || 300
139
140 let i = 0
141 let state = authz === request.headers.get(HEADER.SIG) ? State.Params : State.New
142 let substate = ParamsState.Name
143 let tmpName = ''
144 let tmpValue = ''
145
146 const parsed: ParsedSignature = {
147 scheme: authz === request.headers.get(HEADER.SIG) ? 'Signature' : '',
148 params: {},
149 signingString: '',
150 signature: '',
151 keyId: '',
152 algorithm: '',
153 opaque: '',
154 }
155
156 for (i = 0; i < authz.length; i++) {
157 const c = authz.charAt(i)
158 let code = c.charCodeAt(0)
159
160 switch (Number(state)) {
161 case State.New:
162 if (c !== ' ') parsed.scheme += c
163 else state = State.Params
164 break
165
166 case State.Params:
167 switch (Number(substate)) {
168 case ParamsState.Name:
169 // restricted name of A-Z / a-z

Callers 2

utils.spec.tsFile · 0.90
onRequestFunction · 0.90

Calls 2

validateAlgorithmFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected