-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHTTP.java
More file actions
900 lines (824 loc) · 24.1 KB
/
HTTP.java
File metadata and controls
900 lines (824 loc) · 24.1 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
package javaforce;
/** HTTP Client
*
* @author pquiring
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class HTTP {
protected Socket s;
protected OutputStream os;
protected InputStream is;
protected String host;
protected int port;
private Parameters request_headers = new Parameters();
private Parameters reply_headers = new Parameters();
private String accept = "*/*";
private int code = -1;
public static boolean debug = false;
private Progress progress;
private static int timeout = 30000;
private static String user_agent = "JavaForce.HTTP";
//form types : see RFC 1867, 7568
/** Form Type for URL encoded data. */
public static final String formType = "application/x-www-form-urlencoded";
/** Form Type for multipart data (supports files). */
public static final String formTypeMultiPart = "multipart/form-data";
/** Part Type for plain text. */
public static final String partTypeText = "text/plain";
/** Part Type for html text. */
public static final String partTypeHTML = "text/html";
/** Part Type for binary data. */
public static final String partTypeStream = "application/octet-stream";
/** Part Type for zip file. */
public static final String partTypeZip = "application/x-zip-compressed";
private final static int bufsiz = 1024;
/** HTTP Parameters.
* Stores HTTP Headers or URL Parameters.
*/
public static class Parameters {
private HashMap<String, String> params = new HashMap<>();
public String get(String name) {
return params.get(name);
}
public void put(String name, String value) {
params.put(name, value);
}
public String[] keys() {
return params.keySet().toArray(JF.StringArrayType);
}
public void clear() {
params.clear();
}
public HashMap<String, String> getHashMap() {
return params;
}
/** Decode HTTP headers starting at offset.
* No URL decoding performed.
*/
public static Parameters decode(String[] headers, char equals, int offset) {
Parameters p = new Parameters();
for(int i=offset;i<headers.length;i++) {
String ln = headers[i];
int idx = ln.indexOf(equals);
if (idx == -1) continue;
String key = ln.substring(0, idx).trim();
String value = ln.substring(idx + 1);
if (value.length() > 1) {
value = value.trim();
}
p.put(key, value);
}
return p;
}
/** Decode HTTP headers.
* Searches for headers after first blank line.
* equals = ':'
* No URL decoding performed.
*/
public static Parameters decode(String[] headers) {
for(int i=0;i<headers.length;i++) {
if (headers[i].length() == 0) return decode(headers, ':', i + 1);
}
return null;
}
/** Decode HTTP headers.
* Searches for headers after first blank line.
* No URL decoding performed.
*/
public static Parameters decode(String[] headers, char equals) {
for(int i=0;i<headers.length;i++) {
if (headers[i].length() == 0) return decode(headers, equals, i + 1);
}
return null;
}
/** Decode URL parameters.
* URL decoding is performed.
*/
public static Parameters decode(String query) {
Parameters params = new Parameters();
String[] items = query.split("&");
for(String item : items) {
int idx = item.indexOf('=');
if (idx == -1) continue;
String key = item.substring(0, idx);
String value = item.substring(idx + 1);
params.put(key, JF.decodeURL(value));
}
return params;
}
}
/** Part a of multipart POST */
public static class Part {
public String name;
public String mimeType;
public String filename; //optional
public byte[] data;
}
/** Progress Listener */
public static interface Progress {
/** Invoked to update download progress.
* @param downloaded = progress so far
* @param length = total length (-1 = unknown)
*/
public void progress(long downloaded, long length);
}
protected static class Buffer {
public byte[] buf;
public int pos;
public int count;
public Buffer() {
buf = new byte[bufsiz];
pos = 0;
count = 0;
}
public void expand() {
byte[] newbuf = new byte[buf.length << 1];
if (count > 0) {
System.arraycopy(buf, pos, newbuf, 0, count);
}
buf = newbuf;
pos = 0;
}
public void condense() {
if (pos == 0) return;
if (count == 0) {
pos = 0;
return;
}
System.arraycopy(buf, pos, buf, 0, count);
pos = 0;
}
public void consume(int length) {
if (length > count) {
JFLog.log("HTTP:Error:Buffer.consume(length > count)");
}
pos += length;
count -= length;
if (count == 0) {
pos = 0;
}
}
public void consumeAll() {
pos = 0;
count = 0;
}
public void setLength(int length) {
if (buf.length == length) return;
byte[] newbuf = new byte[length];
if (buf.length > length) {
//copy part
System.arraycopy(buf, pos, newbuf, 0, length);
} else {
//copy all
System.arraycopy(buf, pos, newbuf, 0, count);
}
buf = newbuf;
pos = 0;
}
public void append(Buffer other) {
if (other.count == 0) return;
while (buf.length < count + other.count) {
expand();
}
if (pos > 0) condense();
System.arraycopy(other.buf, other.pos, buf, pos + count, other.count);
count += other.count;
other.consumeAll();
}
public void append(Buffer other, int maxCopy) {
if (other.count == 0) return;
if (maxCopy == 0) return;
int toCopy = maxCopy;
if (other.count < maxCopy) toCopy = other.count;
while (buf.length < count + toCopy) {
expand();
}
if (pos > 0) condense();
System.arraycopy(other.buf, other.pos, buf, pos + count, toCopy);
count += toCopy;
other.consume(toCopy);
}
public byte[] toArray() {
if (count != buf.length) {
return Arrays.copyOfRange(buf, pos, pos + count);
}
return buf;
}
}
/** Removes user info from HTTP URL. */
public static String cleanURL(String url) {
return JF.cleanURL(url);
}
public static String getUserInfo(String urlstr) {
try {
return new URI(urlstr).toURL().getUserInfo();
} catch (Exception e) {
return null;
}
}
public static String getHost(String urlstr) {
try {
return new URI(urlstr).toURL().getHost();
} catch (Exception e) {
return null;
}
}
public static int getPort(String urlstr) {
try {
return new URI(urlstr).toURL().getPort();
} catch (Exception e) {
return 80;
}
}
/** Set connect/read timeout in ms (default = 30000) */
public static void setTimeout(int timeout) {
HTTP.timeout = timeout;
}
public boolean open(String host) {
return open(host, 80);
}
public boolean open(String host, int port) {
this.host = host;
this.port = port;
try {
if (debug) JFLog.log("HTTP.connect:" + host + ":" + port);
s = new Socket();
s.connect(new InetSocketAddress(host, port), timeout);
s.setSoTimeout(timeout);
os = s.getOutputStream();
is = s.getInputStream();
} catch (Exception e) {
JFLog.log(e);
if (s != null) {
try {s.close();} catch (Exception e2) {}
s = null;
}
s = null;
return false;
}
return true;
}
/** Close connection. */
public void close() {
if (s != null) {
try {
s.close();
} catch (Exception e) {
JFLog.log(e);
}
s = null;
}
}
/** Returns connection status. */
public boolean isConnected() {
return s != null;
}
/** Registers a download progress listener. */
public void setProgressListener(Progress progress) {
this.progress = progress;
}
/** Set a request header. */
public void setHeader(String key, String value) {
request_headers.put(key, value);
}
/** Clears all request headers. */
public void clearHeaders() {
request_headers.clear();
}
/** Returns reply header. */
public String getHeader(String key) {
return reply_headers.get(key);
}
/** Returns reply headers. */
public HashMap<String, String> getHeaders() {
return reply_headers.getHashMap();
}
/** Returns last status code. */
public int getCode() {
return code;
}
/** Set Accept header value. */
public void setAccept(String accept) {
this.accept = accept;
}
/** Set User-Agent header value. */
public static void setUserAgent(String ua) {
user_agent = ua;
}
private int read(Buffer buf, int length) throws Exception {
if (buf.count >= length) return length;
int maxlength = buf.buf.length - buf.pos - buf.count;
if (length > maxlength) {
if (buf.pos > 0) {
buf.condense();
maxlength = buf.buf.length - buf.count;
}
}
if (length > maxlength) {
length = maxlength;
if (length == 0) {
JFLog.log("HTTP:Error:buffer full:" + buf.buf.length + "," + buf.pos + "," + buf.count);
return 0;
}
}
int read = is.read(buf.buf, buf.pos + buf.count, length);
if (read > 0) {
buf.count += read;
}
return read;
}
private void write(byte[] data) throws Exception {
os.write(data);
}
private void sendString(String req) throws Exception {
write(req.getBytes());
}
private void sendData(byte[] data) throws Exception {
write(data);
}
private void sendEnter() throws Exception {
write("\r\n".getBytes());
}
private boolean endOfHeaders(Buffer buf) {
if (buf.count < 4) return false;
int length = buf.count;
return ((buf.buf[length-4] == '\r') && (buf.buf[length-3] == '\n') && (buf.buf[length-2] == '\r') && (buf.buf[length-1] == '\n'));
}
private boolean haveChunkLength(Buffer buf) {
if (buf.count < 2) return false;
int length = buf.count;
int start = buf.pos;
int end = buf.pos + length - 1;
for(int pos=start;pos<end;pos++) {
if (buf.buf[pos] == '\r' && buf.buf[pos + 1] == '\n') return true;
}
return false;
}
private int getChunkLength(Buffer buf) {
int value = 0;
while (buf.buf[buf.pos] != '\r') {
value *= 16;
char digit = (char)buf.buf[buf.pos];
if ((digit >= '0') && (digit <= '9')) {
value += (digit - '0');
} else if ((digit >= 'A' && digit <= 'F')) {
value += (digit - 'A' + 10);
} else if ((digit >= 'a' && digit <= 'f')) {
value += (digit - 'a' + 10);
} else {
JFLog.log("HTTP:Error:Invalid chunk length");
return -1;
}
buf.consume(1);
}
buf.consume(2); //consume \r\n
return value;
}
private boolean getReply(OutputStream os) throws Exception {
Buffer buf = new Buffer();
Buffer req = new Buffer();
//read headers
reply_headers.clear();
do {
int read = read(buf, bufsiz);
buf.pos = 0;
buf.count = read;
while (buf.count > 0) {
if (req.count == req.buf.length) {
req.expand();
}
req.buf[req.count++] = buf.buf[buf.pos++];
buf.count--;
read--;
if (endOfHeaders(req)) break;
}
} while (!endOfHeaders(req));
//parse headers
long length = -1;
boolean chunked = false;
String[] lns = new String(req.toArray()).split("\r\n");
for(int i=1;i<lns.length;i++) {
String ln = lns[i];
int idx = ln.indexOf(':');
if (idx == -1) continue;
String key = ln.substring(0, idx).trim();
String value = ln.substring(idx+1);
if (value.length() > 1) {
value = value.trim();
}
reply_headers.put(key, value);
if (debug) System.out.println("Reply:" + key + "=" + value);
switch (key.toLowerCase()) {
case "content-length": length = Long.valueOf(value); break;
case "transfer-encoding": chunked = value.contains("chunked"); break;
}
}
//parse reply : HTTP/1.x code msg
String reply = lns[0];
if (debug) System.out.println("Reply=" + reply);
int i1 = reply.indexOf(' ');
if (i1 == -1) {
JFLog.log("HTTP:Error:Invalid Reply");
return false;
}
int i2 = reply.substring(i1+1).indexOf(' ');
if (i2 == -1) {
JFLog.log("HTTP:Error:Invalid Reply");
return false;
}
code = Integer.valueOf(reply.substring(i1+1, i1+i2+1));
boolean disconn = reply.endsWith("1.0");
if (length == 0) {
if (disconn) close();
return true;
}
long copied = 0;
//read data
if (chunked) {
if (debug) System.out.println("HTTP:Reading Chunked reply");
//read chunked data
while (true) {
if (haveChunkLength(buf)) {
int chunkLength = getChunkLength(buf);
boolean endOfChunks = chunkLength == 0;
//read chunk
if (buf.count > 0) {
int toCopy = buf.count;
if (toCopy > chunkLength) toCopy = chunkLength;
os.write(buf.buf, buf.pos, toCopy);
chunkLength -= toCopy;
buf.consume(toCopy);
copied += toCopy;
}
while (chunkLength > 0) {
int toread = chunkLength;
if (toread > bufsiz) toread = bufsiz;
int read = read(buf, toread);
if (read > 0) {
copied += read;
if (progress != null) {
progress.progress(copied, -1);
}
os.write(buf.buf, buf.pos, read);
chunkLength -= read;
buf.consume(read);
}
}
//read \r\n
while (buf.count < 2) {
read(buf, 2 - buf.count);
}
buf.consume(2);
if (endOfChunks) {
if (disconn) close();
return true;
}
} else {
read(buf, bufsiz);
}
}
} else if (length != -1) {
if (debug) System.out.println("HTTP:Reading Length:" + length);
//read length data
if (buf.count > 0) {
copied += buf.count;
os.write(buf.toArray());
buf.consumeAll();
}
while (copied < length) {
int toread = bufsiz;
if (toread > length) {
toread = (int)length;
}
int read = read(buf, toread);
if (read > 0) {
copied += read;
if (progress != null) {
progress.progress(copied, length);
}
os.write(buf.toArray());
buf.consume(read);
}
}
if (disconn) close();
return true;
} else {
if (debug) System.out.println("HTTP:Reading till carrier dropped");
//read till carrier is dropped
do {
if (buf.count > 0) {
copied += buf.count;
if (progress != null) {
progress.progress(copied, -1);
}
os.write(buf.toArray());
buf.consumeAll();
}
} while (read(buf, bufsiz) >= 0);
if (disconn) close();
return true;
}
}
/** Append user headers. */
private void appendHeaders(StringBuilder req) {
String[] keys = request_headers.keys();
for(String key : keys) {
String value = request_headers.get(key);
req.append(key + ": " + value + "\r\n");
}
}
/** HTTP GET using url.
* Writes content to OutputStream.
*/
public boolean get(String url, OutputStream os) {
if (debug) {
JFLog.log("HTTP.get():" + url);
}
code = -1;
if (s == null) return false;
StringBuilder req = new StringBuilder();
req.append("GET " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Content-Length: 0\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
appendHeaders(req);
req.append("\r\n");
if (debug) {
JFLog.log("request=" + req.toString());
}
try {
sendString(req.toString());
return getReply(os);
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** HTTP GET using url.
* Returns as a byte[]
*/
public byte[] get(String url) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (!get(url, baos)) return null;
return baos.toByteArray();
}
/** HTTP GET using url.
* Returns as a String.
*/
public String getString(String url) {
return new String(get(url));
}
private void sendBoundaryHashes() throws Exception {
write("--".getBytes());
}
private void sendBoundary(String boundary) throws Exception {
sendBoundaryHashes();
write(boundary.getBytes());
}
private String makePart(Part part) {
StringBuilder buf = new StringBuilder();
buf.append(("Content-Disposition: form-data; name=\"" + part.name + "\""));
if (part.filename != null) {
buf.append(("; filename=\"" + part.filename + "\""));
}
buf.append("\r\n");
buf.append(("Content-Type: " + part.mimeType + "\r\n"));
buf.append(("Content-Length: " + part.data.length + "\r\n"));
buf.append("\r\n"); //end of headers
return buf.toString();
}
private void sendPart(Part part) throws Exception {
String buf = makePart(part);
sendString(buf);
}
/** HTTP POST (multipart/form-data encoded)
* Writes content to OutputStream.
*/
public boolean post(String url, Part[] parts, OutputStream os) {
code = -1;
if (s == null) return false;
StringBuilder req = new StringBuilder();
req.append("POST " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
appendHeaders(req);
String boundary = "----JavaForceHTTP" + System.currentTimeMillis();
int boundary_length = boundary.length();
req.append("Content-Type: " + formTypeMultiPart + "; boundary=" + boundary + "\r\n");
long length = 0;
{
boolean first = true;
for(Part part : parts) {
if (first) {
first = false;
} else {
length += 2; //\r\n
}
length += 2 + boundary_length;
length += 2; //\r\n
length += makePart(part).length();
length += part.data.length;
length += 2; //\r\n
length += 2 + boundary_length;
}
length += 4; //hashes + \r\n
}
req.append("Content-Length: " + length + "\r\n");
req.append("\r\n");
try {
sendString(req.toString());
boolean first = true;
for(Part part : parts) {
if (first) {
first = false;
} else {
sendEnter();
}
sendBoundary(boundary);
sendEnter();
sendPart(part);
sendData(part.data);
sendEnter();
sendBoundary(boundary);
}
sendBoundaryHashes(); //end of transmission
sendEnter();
return getReply(os);
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** HTTP POST (multipart/form-data encoded)
* @return content (null on error)
*/
public byte[] post(String url, Part[] parts) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (!post(url, parts, baos)) return null;
return baos.toByteArray();
}
/** HTTP POST (URL encoded)
* Writes content to OutputStream.
*/
public boolean post(String url, byte[] data, OutputStream os, String encType) {
code = -1;
if (s == null) return false;
StringBuilder req = new StringBuilder();
req.append("POST " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
if (encType == null) {
encType = "application/x-www-form-urlencoded";
}
req.append("Content-Type: " + encType + "\r\n");
req.append("Content-Length: " + data.length + "\r\n");
appendHeaders(req);
req.append("\r\n");
try {
sendString(req.toString());
sendData(data);
return getReply(os);
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** HTTP POST (URL encoded)
* Writes content to OutputStream.
*/
public boolean post(String url, byte[] data, OutputStream os) {
return post(url, data, os, null);
}
/** HTTP POST (URL encoded)
* Returns content as byte[]
*/
public byte[] post(String url, byte[] data) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (!post(url, data, baos, null)) return null;
return baos.toByteArray();
}
/** HTTP POST (URL encoded)
* Returns content as byte[]
*/
public byte[] post(String url, byte[] data, String encType) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (!post(url, data, baos, encType)) return null;
return baos.toByteArray();
}
/** HTTP POST using url with post data using enctype = application/x-www-form-urlencoded.
* Returns content as String.
*/
public String postString(String url, byte[] data) {
return new String(post(url, data));
}
/** Test HTTP */
public static void main(String[] args) {
HTTP http = new HTTP();
String html;
byte[] data;
boolean print = false;
if (args.length > 0 && args[0].equals("print")) print = true;
debug = true;
http.open("google.com");
html = http.getString("/");
http.close();
if (html == null || html.length() == 0) {
System.out.println("Error:HTTP.get() failed");
}
if (print) System.out.println(html);
http.open("www.google.com");
html = http.getString("/");
http.close();
if (html == null || html.length() == 0) {
System.out.println("Error:HTTP.get() failed");
}
if (print) System.out.println(html);
//test post file to example.com
Part part = new Part();
part.name = "file";
part.filename = "file.zip";
part.data = new byte[] {1,2,3};
part.mimeType = partTypeStream;
http.open("example.com");
data = http.post("/upload", new Part[] {part});
http.close();
if (data == null || data.length == 0) {
System.out.println("Error:HTTP.post() failed");
return;
}
if (print) {
System.out.println(new String(data));
}
}
/**
* Returns HTTP style parameter from list of parameters.
*
* @param params = HTTP list of parameters
* @param name = name of parameter to return
* @return parameter
*/
public static String getParameter(String[] params, String name) {
for(int a=0;a<params.length;a++) {
String param = params[a];
int idx = param.indexOf(":");
if (idx == -1) continue;
String key = param.substring(0, idx).trim();
String value = param.substring(idx + 1);
if (value.length() > 1) {
value = value.trim();
}
if (key.equalsIgnoreCase(name)) {
return value;
}
}
return null;
}
/**
* Returns HTTP style parameter(s) from list of parameters.
*
* @param params = HTTP list of parameters
* @param name = name of parameter to return
* @return parameter(s)
*/
public static String[] getParameters(String[] params, String name) {
ArrayList<String> lst = new ArrayList<>();
for(int a=0;a<params.length;a++) {
String param = params[a];
int idx = param.indexOf(":");
if (idx == -1) continue;
String key = param.substring(0, idx).trim();
String value = param.substring(idx + 1);
if (value.length() > 1) {
value = value.trim();
}
if (key.equalsIgnoreCase(name)) {
lst.add(value);
}
}
return lst.toArray(JF.StringArrayType);
}
/**
* Get Content from a HTTP message.
*/
public static String[] getContent(String[] msg) {
ArrayList<String> content = new ArrayList<String>();
for (int a = 0; a < msg.length; a++) {
//look for double \r\n (which appears as an empty line) that marks end of headers
if (msg[a].length() == 0) {
for (int b = a + 1; b < msg.length; b++) {
content.add(msg[b]);
}
break;
}
}
return content.toArray(JF.StringArrayType);
}
}