Skip to content

Commit

Permalink
gh-29 Ignore case in HTTP response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonmcmurray committed Jun 5, 2020
1 parent aef2b3c commit 508f292
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ tab & extract to your `QPATH` directory.
Using `qDoc` from Kx (included with Kx Developer)

```bash
$ q $AXLIBRARIES_HOME/ws/qdoc.q_ -src req/ -out docs/api/
$ # -norecurse so as not to go into ext/ dir
$ q $AXLIBRARIES_HOME/ws/qdoc.q_ -src req/ -out docs/api/ -norecurse
..
$ mkdocs serve # to preview locally
..
Expand Down
2 changes: 1 addition & 1 deletion req/auth.q
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
getauth:{[h;u] /h-headers,u-URL
/* prompt for user & pass when site requests basic auth */
h:upper[key h]!value h; //upper case header names
if[not h[`$"WWW-AUTHENTICATE"] like "Basic *";'"unsupported auth challenge"]; //check it needs basic auth
if[not h[`$"www-authenticate"] like "Basic *";'"unsupported auth challenge"]; //check it needs basic auth
-1"Site requested basic auth\nWARNING: user & pass will show in plain text\n"; //warn user before they type pass
1"User: ";s:read0 0; //get username
1"Pass: ";p:read0 0; //get password
Expand Down
9 changes: 5 additions & 4 deletions req/req.q
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ formatresp:{[r]
p:(0,4+first r ss 4#"\r\n") cut r; //split response headers & body
p:@[p;0;"statustext:",]; //add key for status text line
d:trim enlist[`]_(!/)("S:\n")0:p[0]except"\r"; //create dictionary of response headers
d:lower[key d]!value d; //make headers always lower case
d[`status]:"I"$(" "vs r)1; //add status code
:(d;p[1]); //return header dict & reponse body
}
Expand Down Expand Up @@ -117,11 +118,11 @@ send:{[m;u;hd;p;v]
if[v;-1"-- REQUEST --\n",string[hs],"\n",d]; //if verbose, log request
if[v;-1"-- RESPONSE --\n",r,("\n"<>last r)#"\n"]; //if verbose, log response
r:formatresp r; //format response to headers & body
if[(sc:`$"Set-Cookie") in k:key r 0; //check for Set-Cookie headers
if[(sc:`$"set-cookie") in k:key r 0; //check for Set-Cookie headers
.cookie.addcookie[q[`url;`host]]'[value[r 0]where k=sc]]; //set any cookies necessary
if[r[0][`status]=401;:.z.s[m;.auth.getauth[r 0;u];hd;p;v]]; //if unauthorised prompt for user/pass FIX:should have some counter to prevent infinite loops
if[.status.class[r] = 3; //if status is 3XX, redirect
lo:$["/"=r[0][`Location]0;.url.format[`protocol`auth`host#q`url],1_r[0]`Location;r[0]`Location]; //detect if relative or absolute redirect
lo:$["/"=r[0][`location]0;.url.format[`protocol`auth`host#q`url],1_r[0]`location;r[0]`location]; //detect if relative or absolute redirect
:.z.s[m;lo;hd;p;v]]; //perform redirections if needed
:r;
}
Expand All @@ -133,10 +134,10 @@ send:{[m;u;hd;p;v]
// @return {any} Parsed response
parseresp:{[r]
/ TODO - add handling for other data types? /
eh:`$"Content-Encoding";
eh:`$"content-encoding";
if[(.z.K>=3.7)&r[0][eh]like"gzip";:.z.s(enlist[eh]_;-35!)@'r]; //decompress gzip response on 3.7+
if[eh in key r 0;'"Unsupported encoding: ",r[0]eh]; //if other encoding, or not 3.7, signal
:$[(`j in key`)&r[0][`$"Content-Type"]like .req.ty[`json],"*";.j.k;] r[1]; //check for JSON, parse if so
:$[(`j in key`)&r[0][`$"content-type"]like .req.ty[`json],"*";.j.k;] r[1]; //check for JSON, parse if so
}

// @kind function
Expand Down

0 comments on commit 508f292

Please sign in to comment.