Skip to content

Commit 401dc3d

Browse files
authored
#48 multipart/form-data support (#49)
* #48 multipart/form-data support * Tweak boundary to make Aaron happy * Cleanup multipart
1 parent fdafaed commit 401dc3d

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

source/HttpCommand.dyalog

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
rVersion
88
Return the current version
99
:Access public shared
10-
r'HttpCommand' '5.5.0' '2024-03-07'
10+
r'HttpCommand' '5.6.0' '2024-03-17'
1111
1212

1313
Request-related fields
@@ -405,7 +405,7 @@
405405
∆FAIL:(rc secureParams)¯1 msg failure
406406
407407

408-
{r}certs HttpCmd args;url;parms;hdrs;urlparms;p;b;secure;port;host;path;auth;req;err;done;data;datalen;rc;donetime;ind;len;obj;evt;dat;z;msg;timedOut;certfile;keyfile;simpleChar;defaultPort;cookies;domain;t;replace;outFile;toFile;startSize;options;congaPath;progress;starttime;outTn;secureParams;ct;forceClose;headers;cmd;file;protocol;conx;proxied;proxy;cert;noCT;simpleParms;noContentLength;connectionClose;tmpFile;tmpTn;redirected;encoding;compType;isutf8
408+
{r}certs HttpCmd args;url;parms;hdrs;urlparms;p;b;secure;port;host;path;auth;req;err;done;data;datalen;rc;donetime;ind;len;obj;evt;dat;z;msg;timedOut;certfile;keyfile;simpleChar;defaultPort;cookies;domain;t;replace;outFile;toFile;startSize;options;congaPath;progress;starttime;outTn;secureParams;ct;forceClose;headers;cmd;file;protocol;conx;proxied;proxy;cert;noCT;simpleParms;noContentLength;connectionClose;tmpFile;tmpTn;redirected;encoding;compType;isutf8;boundary
409409
issue an HTTP command
410410
certs - X509Cert|(PublicCertFile PrivateKeyFile) SSLValidation Priority PublicCertFile PrivateKeyFile
411411
args - [1] HTTP method
@@ -547,6 +547,17 @@
547547
:Else
548548
parmsSafeJSON parms
549549
:EndIf
550+
:Case 'multipart/form-data'
551+
:If 9.1nameClass parms must be a namespace
552+
∆ENDr.msg'Params must be a namespace when using "multipart/form-data" content type'
553+
:Else
554+
boundary'--',32{[?]}⎕D,⎕A,⎕C ⎕A
555+
hdrs'Content-Type'(hdrs setHeader)'multipart/form-data; boundary=',boundary
556+
(parms msg)boundary multipart parms
557+
:If ~0msg
558+
∆ENDr.msgmsg
559+
:EndIf
560+
:EndIf
550561
:Else
551562
parmsparms
552563
:EndSelect
@@ -900,7 +911,7 @@
900911
:EndIf
901912
:Else
902913
:If 1081=rc 1081 could be due to an error in Conga that fails on long URLs, so try sending request as a character vector
903-
:If 0=rcLDRC.Send Client(cmd,' ',(path,(0urlparms)'?',urlparms),' HTTP/1.1',(⎕UCS 13 10),(': '(⎕UCS 13 10),¨1hdrs),(⎕UCS 13 10),parms)
914+
:If 0=rcLDRC.Send Client(cmd,' ',(path,(0urlparms)'?',urlparms),' HTTP/1.1',NL,(': 'NL,¨1hdrs),NL,parms)
904915
∆LISTEN
905916
:EndIf
906917
:EndIf
@@ -956,6 +967,49 @@
956967
:EndTrap
957968
958969

970+
(payload msg)boundary multipart parms;name;value;filename;contentType;content
971+
format multipart/form-data payload
972+
parms is a namespace with named objects
973+
974+
msgpayload''
975+
:For name :In parms.⎕NL ¯2
976+
payload,boundary
977+
(value contentType)2(parmsname),''
978+
payload,NL,'Content-Disposition: form-data; name="',name,'"'
979+
:If ~0contentType
980+
payload,NL,'Content-Type: ',contentType
981+
:EndIf
982+
:If '@<'value
983+
:If ⎕NEXISTS 1value
984+
:AndIf 2=1 ⎕NINFO 1value
985+
payload,('@'=value)/'; filename="',(¯21 ⎕NPARTS value),'"'
986+
(contentType content)contentType readFile 1value
987+
payload,NL,'Content-Type: ',contentType,NL,NL
988+
payload,content,NL
989+
:Else
990+
0msg'File not found: "',(1value),'"'
991+
:EndIf
992+
:Else
993+
payload,NL,NL,(value),NL
994+
:EndIf
995+
:EndFor
996+
payload,boundary,'--'
997+
998+
999+
(contentType content)contentType readFile filename;ext;tn
1000+
return file content in a manner consistent with multipart/form-data
1001+
:Access public shared
1002+
:If 0contentType
1003+
ext⎕C 31 ⎕NPARTS filename
1004+
:If ext'.txt' contentType'text/plain'
1005+
:Else contentType'application/octet-stream'
1006+
:EndIf
1007+
:EndIf
1008+
tnfilename ⎕NTIE 0
1009+
content⎕NREAD tn,(⎕DR''),¯1
1010+
⎕NUNTIE tn
1011+
1012+
9591013
NL⎕UCS 13 10
9601014
toChar{(⎕DR'')⎕DR }
9611015
fromutf8{0::(⎕AV,'?')[⎕AVU] 'UTF-8'⎕UCS } Turn raw UTF-8 input into text

0 commit comments

Comments
 (0)