Advertisement
Guest User

Untitled

a guest
Oct 8th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
q/kdb+ 2.41 KB | None | 0 0
  1. assoctomap:{x[;0]!x[;1]}
  2. htmlcharassoc:(("&";"&amp;");(" ";"&nbsp;");("'";"&#39;");("\"";"&quot;");("<";"&lt;");(">";"&gt;"))
  3. htmlmap:assoctomap htmlcharassoc
  4. convertvia:{[map;thing] raze {f:x[y];$[""~f;y;f]}[map;] each thing}
  5. htmlescape:{convertvia[htmlmap;x]}
  6. /urlcharassoc:(("+";" ");("x";"x"))
  7. /urlmap:assoctomap urlcharassoc
  8. conv:{y sv x vs z}
  9. fromurl:{conv["%2C";","] conv["%20";" "] conv["+";" "] x}
  10. attrtostring:{[a]$[a~();"";" "sv{string[x],"=\"",y[x],"\""}[;a] each key a]}
  11. tag:{[namesym;attrib]"<",string[namesym]," ",attrtostring[attrib],">"}
  12. etag:{"</",string[x],">"}
  13. htmli:{t:type x;c:count x;
  14.        $[t;t;$[c=2;tag[x[0];()];tag[x[0];x[1]]],$[type x[c-1];htmlescape x[c-1];$[type x[c-1][0];html x[c-1];html each x[c-1]]],etag x[0]]}
  15. html:{raze htmli x}
  16. successhtml:"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"
  17. page:{successhtml,"\r\n",html x}
  18. pageh:{successhtml,sv["\r\n";(string each key x) ,' ,[": "] each (value x)],"\r\n\r\n", html y}
  19. / need cookie stuff
  20. / Set-Cookie: HSID=AYQEVn….DKrdst; Domain=.foo.com; Path=/; Expires=Wed, 13 Jan 2021 22:23:01 GMT; HttpOnly
  21. / MaxAge or expiry (forgot exact name for "expiry")
  22. params:{assoctomap each[fromurl] each vs["="] each vs["&"] vs["?";x[0]][1]}
  23. cookies:{c:x[1][`Cookie];$[""~c;0;assoctomap vs["="] each  "; " vs c]}
  24.  
  25.  
  26. pages:(("home";
  27.         {page (`html;
  28.                ((`h1;"WELCOME TO THE HOMEPAGE");
  29.                 (`form;`id`action!("search";"/greet");
  30.                       ((`input;(enlist `name)!enlist"name";());
  31.                        (`input;`type`value!("submit";"say hello");())));
  32.                 (`body;(`p;"this page is awesome"))))});
  33.        ("data.tsv";{"HTTP/1.1 200 OK\r\nContent-Type: text/tsv\r\n\r\n","\n"sv read0 `:data.tsv});
  34.        ("greet";
  35.         {page (`html;
  36.                (`h1; "greetings ", 0N!params[x]["name"],"!!!"))});
  37.        ("cookies";
  38.         {page (`html; .Q.s cookies x)});
  39.        ("newcookie";
  40.         {pageh[enlist[`$"Set-Cookie"]!enlist "value=",(string rand 1000),"; Path=/cookies;"]
  41.          (`html;(`h1; "This Page Gives You A Cookie!"))});
  42.        ("another";
  43.         {"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n", raze sv["\n";read0 `:graph.js], html (`html;
  44.                ((`h1;"ANOTHER PAGE");
  45.                 (`body;(`p;"this is for stuff that doesn't fit on the homepage"))))}))
  46. p:assoctomap pages
  47.  
  48. .z.ph:{0N!x[0];r:p[vs["?";x[0]][0]];$[100h~type r;r[x];"UNKNOWN PAGE"]}
  49. .z.w:{}
  50. .z.ws:{}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement