-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHTTP.rbbas
1664 lines (1491 loc) · 59.1 KB
/
HTTP.rbbas
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#tag Module
Protected Module HTTP
#tag Method, Flags = &h1
Protected Function CodeToMessage(Code As Integer) As String
Select Case Code
Case 100
Return "Continue"
Case 101
Return "Switching Protocols"
Case 102
Return "Processing"
Case 200
Return "OK"
Case 201
Return "Created"
Case 202
Return "Accepted"
Case 203
Return "Non-Authoritative Information"
Case 204
Return "No Content"
Case 205
Return "Reset Content"
Case 206
Return "Partial Content"
Case 207
Return "Multi-Status"
Case 208
Return "Already Reported"
Case 226
Return "IM Used"
Case 300
Return "Multiple Choices"
Case 301
Return "Moved Permanently"
Case 302
Return "Found"
Case 303
Return "See Other"
Case 304
Return "Not Modified"
Case 305
Return "Use Proxy"
Case 306
' This status code is deprecated by http://tools.ietf.org/html/rfc7231#section-6.4.6
Return "Switch Proxy"
Case 307
Return "Temporary Redirect" ' http://tools.ietf.org/html/rfc7231#section-6.4.7
Case 308 ' https://tools.ietf.org/html/draft-reschke-http-status-308-07
Return "Permanent Redirect"
Case 400
Return "Bad Request"
Case 401
Return "Unauthorized"
Case 402
Return "Payment Required" ' http://tools.ietf.org/html/rfc7231#section-6.5.2
Case 403
Return "Forbidden"
Case 404
Return "Not Found"
Case 405
Return "Method Not Allowed"
Case 406
Return "Not Acceptable"
Case 407
Return "Proxy Authentication Required"
Case 408
Return "Request Timeout"
Case 409
Return "Conflict"
Case 410
Return "Gone"
Case 411
Return "Length Required"
Case 412
Return "Precondition Failed"
Case 413
Return "Request Entity Too Large"
Case 414
Return "Request-URI Too Long"
Case 415
Return "Unsupported Media Type"
Case 416
Return "Requested Range Not Satisfiable"
Case 417
Return "Expectation Failed"
Case 418
Return "I'm a teapot" ' https://tools.ietf.org/html/rfc2324
Case 420
Return "Enhance Your Calm" 'Nonstandard, from Twitter API
Case 422
Return "Unprocessable Entity"
Case 423
Return "Locked"
Case 424
Return "Failed Dependency"
Case 425
Return "Unordered Collection" 'Draft, https://tools.ietf.org/html/rfc3648
Case 426
Return "Upgrade Required"
Case 428
Return "Precondition Required"
Case 429
Return "Too Many Requests"
Case 431
Return "Request Header Fields Too Large"
Case 444
Return "No Response" 'Nginx
Case 449
Return "Retry With" 'Nonstandard, from Microsoft http://msdn.microsoft.com/en-us/library/dd891478.aspx
Case 450
Return "Blocked By Windows Parental Controls" 'Nonstandard, from Microsoft
Case 451
Return "Unavailable For Legal Reasons" 'Draft, https://tools.ietf.org/html/draft-tbray-http-legally-restricted-status-00
Case 463
Return "Restricted Client" ' non-standard, used by CDNs
Case 494
Return "Request Header Too Large" 'nginx
Case 495
Return "Cert Error" 'nginx
Case 496
Return "No Cert" 'nginx
Case 497
Return "HTTP to HTTPS" 'nginx
Case 499
Return "Client Closed Request" 'nginx
Case 500
Return "Internal Server Error"
Case 501
Return "Not Implemented"
Case 502
Return "Bad Gateway"
Case 503
Return "Service Unavailable"
Case 504
Return "Gateway Timeout"
Case 505
Return "HTTP Version Not Supported"
Case 506
Return "Variant Also Negotiates" 'WEBDAV https://tools.ietf.org/html/rfc2295
Case 507
Return "Insufficient Storage" 'WEBDAV https://tools.ietf.org/html/rfc4918
Case 508
Return "Loop Detected" 'WEBDAV https://tools.ietf.org/html/rfc5842
Case 509
Return "Bandwidth Limit Exceeded" 'Apache, others
Case 510
Return "Not Extended" 'https://tools.ietf.org/html/rfc2774
Case 511
Return "Network Authentication Required" 'https://tools.ietf.org/html/rfc6585
Else
Return "Unknown Status Code"
End Select
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function CRLF() As String
Return EndOfLine.Windows
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function DateString(d As Date) As String
Dim dt As String
d.GMTOffset = 0
Select Case d.DayOfWeek
Case 1
dt = dt + "Sun, "
Case 2
dt = dt + "Mon, "
Case 3
dt = dt + "Tue, "
Case 4
dt = dt + "Wed, "
Case 5
dt = dt + "Thu, "
Case 6
dt = dt + "Fri, "
Case 7
dt = dt + "Sat, "
End Select
dt = dt + Format(d.Day, "00") + " "
Select Case d.Month
Case 1
dt = dt + "Jan "
Case 2
dt = dt + "Feb "
Case 3
dt = dt + "Mar "
Case 4
dt = dt + "Apr "
Case 5
dt = dt + "May "
Case 6
dt = dt + "Jun "
Case 7
dt = dt + "Jul "
Case 8
dt = dt + "Aug "
Case 9
dt = dt + "Sep "
Case 10
dt = dt + "Oct "
Case 11
dt = dt + "Nov "
Case 12
dt = dt + "Dec "
End Select
dt = dt + Format(d.Year, "0000") + " " + Format(d.Hour, "00") + ":" + Format(d.Minute, "00") + ":" + Format(d.Second, "00") + " GMT"
Return dt
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function DateString(Data As String) As Date
'Sat, 29 Oct 1994 19:43:31 GMT
Data = ReplaceAll(Data, "-", " ")
Dim d As Date
Dim members() As String = Split(Data, " ")
If UBound(members) = 5 Then
Dim dom, mon, year, h, m, s, tz As Integer
dom = Val(members(1))
Select Case members(2)
Case "Jan"
mon = 1
Case "Feb"
mon = 2
Case "Mar"
mon = 3
Case "Apr"
mon = 4
Case "May"
mon = 5
Case "Jun"
mon = 6
Case "Jul"
mon = 7
Case "Aug"
mon = 8
Case "Sep"
mon = 9
Case "Oct"
mon = 10
Case "Nov"
mon = 11
Case "Dec"
mon = 12
End Select
year = Val(members(3))
Dim time As String = members(4)
h = Val(NthField(time, ":", 1))
m = Val(NthField(time, ":", 2))
s = Val(NthField(time, ":", 3))
tz = Val(members(5))
d = New Date(year, mon, dom, h, m, s, tz)
End If
Return d
End Function
#tag EndMethod
#tag DelegateDeclaration, Flags = &h1
Protected Delegate Sub DebugMessage(Message As Variant, Level As Integer)
#tag EndDelegateDeclaration
#tag Method, Flags = &h1
Protected Function DecodeChunkedData(Data As MemoryBlock) As MemoryBlock
Dim instream As Readable = New BinaryStream(Data)
Dim output As New MemoryBlock(0)
Dim outstream As New BinaryStream(output)
Dim chunk As ChunkedStream = ChunkedStream.Open(instream)
Do Until chunk.EOF
outstream.Write(chunk.Read(64))
Loop
outstream.Close
Return output
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function DecompressData(Data As MemoryBlock, AllegedType As String, DebugOutput As HTTP.DebugMessage = Nil) As MemoryBlock
Dim out As MemoryBlock
#pragma BreakOnExceptions Off
If DebugOutput = Nil Then DebugOutput = AddressOf DevNull
Try
out = zlib.GUnZip(Data)
If out <> Nil And AllegedType <> "gzip" Then
DebugOutput.Invoke("Alert: The message claims to be compressed using " + AllegedType + ", but is actually using gzip.", -1)
End If
Catch
End Try
If out = Nil Then
Try
out = zlib.Inflate(Data)
If out <> Nil And AllegedType <> "deflate" Then
DebugOutput.Invoke("Alert: The message claims to be compressed using " + AllegedType + ", but is actually using deflate.", -1)
End If
Catch
End Try
End If
If out = Nil Then
Try
out = zlib.Inflate(Data, Nil, zlib.RAW_ENCODING)
If DebugOutput <> Nil Then
If out = Nil Then
DebugOutput.Invoke("Warning: The message claims to be compressed but does not appear to be in a standard compression format.", -1)
Else
DebugOutput.Invoke("Alert: The message is a raw deflate stream. Inflation was successful anyway, but this is non-standard.", -1)
End If
End If
Catch
End Try
End If
If out = Nil And InStrB(Left(Data, 11), CRLF) > 0 Then
' decompression failed, maybe it's chunked
out = DecompressData(DecodeChunkedData(Data), AllegedType, DebugOutput)
ElseIf out = Nil And DebugOutput <> Nil Then
If AllegedType = "deflate" Then
DebugOutput.Invoke("Alert: The message claims to be deflated but it does not appear to contain valid deflate data.", -1)
ElseIf AllegedType = "gzip" Then
DebugOutput.Invoke("Alert: The message claims to be gzipped but it does not appear to contain valid gzip data.", -1)
Else
DebugOutput.Invoke("Warning: The message claims to be compressed using an unknown, non-standard encoding ('" + AllegedType + "').", -1)
End If
End If
Return out
End Function
#tag EndMethod
#tag Method, Flags = &h21
Attributes( hidden ) Private Sub DevNull(Message As Variant, Level As Integer)
#pragma Unused Message
#pragma Unused Level
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Function ErrorPage(ErrorNumber As Integer, RedirectLink As String = "") As HTTP.Response
Static ErrorPages As Dictionary
If ErrorPages = Nil Then
ErrorPages = New Dictionary
For error As Integer = 100 To 599
Dim page As String = BlankErrorPage
Dim msg As String = CodeToMessage(error)
page = ReplaceAll(page, "%HTTPERROR%", Str(error) + " " + msg)
Select Case error
Case 100
page = ReplaceAll(page, "%DOCUMENT%", "You may now send the next part of your request.")
Case 101
page = ReplaceAll(page, "%DOCUMENT%", "Your request to change protocols is accepted.")
Case 200
page = ReplaceAll(page, "%DOCUMENT%", "Your request was processed successfully.")
Case 201
page = ReplaceAll(page, "%DOCUMENT%", "This resource was created successfully.")
Case 202
page = ReplaceAll(page, "%DOCUMENT%", "Your request was accepted for processing.")
Case 204
page = ReplaceAll(page, "%DOCUMENT%", "This resource is intentionally blank.")
Case 301, 308
page = ReplaceAll(page, "%DOCUMENT%", "This resource has permanently moved; please update your links. <a href=""%REDIR_LINK%"">Click here</a> if you are not automatically redirected.")
Case 302, 307
page = ReplaceAll(page, "%DOCUMENT%", "This resource has temporarily moved. <a href=""%REDIR_LINK%"">Click here</a> if you are not automatically redirected.")
Case 303
page = ReplaceAll(page, "%DOCUMENT%", "Refer to the resource <a href=""%REDIR_LINK%"">here</a> to fulfill your request.")
Case 304
page = ReplaceAll(page, "%DOCUMENT%", "This resource has not been recently modified.")
Case 400
page = ReplaceAll(page, "%DOCUMENT%", "The server did not understand your request.")
Case 402
page = ReplaceAll(page, "%DOCUMENT%", "Access to this resource requires payment. <a href=""%REDIR_LINK%"">Click here</a> to purchase access.")
Case 403, 401
page = ReplaceAll(page, "%DOCUMENT%", "Permission to access this resource is denied.")
Case 404
page = ReplaceAll(page, "%DOCUMENT%", "This resource could not be found.")
Case 405
page = ReplaceAll(page, "%DOCUMENT%", "That request method is not allowed for this resource.")
Case 406
page = ReplaceAll(page, "%DOCUMENT%", "Your browser did not specify an acceptable Content-Type that is compatible with this resource.")
Case 410
page = ReplaceAll(page, "%DOCUMENT%", "This resource has been removed.")
Case 411
page = ReplaceAll(page, "%DOCUMENT%", "Your browser did not specify the length of the request payload.")
Case 413
page = ReplaceAll(page, "%DOCUMENT%", "The request payload is too large.")
Case 414
page = ReplaceAll(page, "%DOCUMENT%", "The request URL is too long.")
Case 415
page = ReplaceAll(page, "%DOCUMENT%", "The request payload is of an unknown or unsupported type.")
Case 416
page = ReplaceAll(page, "%DOCUMENT%", "This resource does not contain the requested range.")
Case 418
page = ReplaceAll(page, "%DOCUMENT%", "I'm a little teapot, short and stout; here is my handle, here is my spout.")
Case 426
page = ReplaceAll(page, "%DOCUMENT%", "This resource is not available via the current network protocol.")
Case 429, 420
page = ReplaceAll(page, "%DOCUMENT%", "Your browser has made too many requests of this server.")
Case 451
page = ReplaceAll(page, "%DOCUMENT%", "This resource is unavailable as a result of a <a href=""%REDIR_LINK%"">legal demand</a>.")
Case 500
page = ReplaceAll(page, "%DOCUMENT%", "An error ocurred while processing your request.")
Case 501
page = ReplaceAll(page, "%DOCUMENT%", "Your browser used a request method that is not implemented by this server.")
Case 503
page = ReplaceAll(page, "%DOCUMENT%", "This server is currently unavailable to process your requst.")
Case 505
page = ReplaceAll(page, "%DOCUMENT%", "Your browser specified an HTTP version that is not supported by this server.")
Case 509
page = ReplaceAll(page, "%DOCUMENT%", "The bandwidth limit for this server has been exceeded.")
Else
page = ReplaceAll(page, "%DOCUMENT%", "No further information is available.")
End Select
page = ReplaceAll(page, "%SIGNATURE%", "<em>Powered By " + DaemonVersion + "</em><br />")
If page.LenB < 512 Then
page = page + "<!--"
Do
page = page + " padding to make IE happy. "
Loop Until page.LenB >= 512
page = page + "-->"
End If
ErrorPages.Value(error) = page
Next
End If
Dim errpage As HTTP.Response = ""
errpage.StatusCode = ErrorNumber
errpage.MessageBody = ErrorPages.Value(ErrorNumber).StringValue
If RedirectLink <> "" Then
errpage.MessageBody = ReplaceAll(errpage.MessageBody, "%REDIR_LINK%", RedirectLink)
errpage.Header("Location") = RedirectLink
End If
errpage.Header("Content-Length") = Str(errpage.MessageBody.LenB)
errpage.Header("Content-Type") = "text/html"
Return errpage
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function FindFile(RootDirectory As FolderItem, FilePath As String) As FolderItem
Dim out As FolderItem = RootDirectory
Dim rootpath As String = RootDirectory.AbsolutePath
For i As Integer = 1 To CountFields(FilePath, "/")
Dim element As String = DecodeURLComponent(NthField(FilePath, "/", i))
If element = "" Then Continue
Select Case element.Trim
Case ".." ' up one
If out.Parent = Nil Then Return Nil ' cannot go up from the volume root
Dim pp As String = out.Parent.AbsolutePath
If Left(pp, rootpath.Len) <> rootpath Then Return Nil ' not contained within root
out = out.Parent
Case ".", "" ' current
out = out ' No-op
Case Else
out = out.Child(element)
If Not out.Exists Then Return Nil
End Select
Next
Return out
Exception
Return Nil
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function FormatBytes(bytes As UInt64, precision As Integer = 2) As String
'Converts raw byte counts into SI formatted strings. 1KB = 1024 bytes.
'Optionally pass an integer representing the number of decimal places to return. The default is two decimal places. You may specify
'between 0 and 16 decimal places. Specifying more than 16 will append extra zeros to make up the length. Passing 0
'shows no decimal places and no decimal point.
Const kilo = 1024
Static mega As UInt64 = kilo * kilo
Static giga As UInt64 = kilo * mega
Static tera As UInt64 = kilo * giga
Static peta As UInt64 = kilo * tera
Static exab As UInt64 = kilo * peta
Dim suffix, precisionZeros As String
Dim strBytes As Double
If bytes < kilo Then
strbytes = bytes
suffix = "bytes"
ElseIf bytes >= kilo And bytes < mega Then
strbytes = bytes / kilo
suffix = "KB"
ElseIf bytes >= mega And bytes < giga Then
strbytes = bytes / mega
suffix = "MB"
ElseIf bytes >= giga And bytes < tera Then
strbytes = bytes / giga
suffix = "GB"
ElseIf bytes >= tera And bytes < peta Then
strbytes = bytes / tera
suffix = "TB"
ElseIf bytes >= tera And bytes < exab Then
strbytes = bytes / peta
suffix = "PB"
ElseIf bytes >= exab Then
strbytes = bytes / exab
suffix = "EB"
End If
While precisionZeros.Len < precision
precisionZeros = precisionZeros + "0"
Wend
If precisionZeros.Trim <> "" Then precisionZeros = "." + precisionZeros
Return Format(strBytes, "#,###0" + precisionZeros) + suffix
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function FormatSocketError(ErrorCode As Integer) As String
Dim err As String = "Socket error: "
Select Case ErrorCode
Case 102
err = err + "Disconnected."
Case 100
err = err + "Failed to create the socket."
Case 103
err = err + "The host name cannot be resolved."
Case 105
err = err + "The port number is already in use."
Case 106
err = err + "The socket is not ready for that command."
Case 107
err = err + "The port number is invalid or restricted."
Case 108
err = err + "Out of memory."
Else
err = err + "Unknown error: number " + Str(ErrorCode)
End Select
Return err
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function IsRobotBlocked(robotstxt As String, UserAgent As String, Path As String, ByRef RetValue As Pair, DebugOutput As HTTP.DebugMessage) As Boolean
'Parses a robots.txt file and returns a Pair containing the UserAgent:Path in the robots.txt that matches the UserAgent and Path, if any.
'If not disallowed (i.e. allowed) then returns NIL.
Const AllBots = "*"
robotstxt = ReplaceLineEndings(robotstxt, EndOfLine.Windows)
Dim records() As String = robotstxt.Split(EndOfLine.Windows + EndOfLine.Windows) 'Robots.txt is broken into records by CRLF+CRLF
'First parse the raw robots.txt
For i As Integer = 0 To UBound(records)
Dim UA(), paths(), lines() As String
lines = Split(records(i), EndOfLine.Windows) 'Each record is broken into members by CRLF
For Each line As String In lines
line = Left(line, line.Len - InStr(line, "#"))
If line.Trim = "" Or Left(line, 1) = "#" Then Continue 'comment lines are ignored
Dim field, value As String
'Each member is broken into halves by a colon (:)
field = NthField(line, ":", 1).Trim
value = NthField(line, ":", 2).Trim
Select Case field.Trim
Case "User-Agent"
UA.Append(value)
Case "Disallow"
If value.Trim = "" Then Continue ' disallows nothing
paths.Append(value)
Case "Sitemap", "Crawl-delay", "Allow"
Continue 'Sometimes used (not an error), but not interesting to us
Else
If DebugOutput <> Nil Then DebugOutput.Invoke("Alert: This website does not have a valid robots.txt file (probably got an error page.)", -1)
Return False
End Select
Next
'Then check to see if we're blocked
For Each Agent As String In UA
If Agent = UserAgent Or Agent.Trim = AllBots Then
For Each URL As String In paths
If InStr(URL, AllBots) > 1 Then
Dim l, r As String
l = NthField(URL, AllBots, 1)
r = NthField(URL, AllBots, 2)
If Left(path, l.Len) = l And Right(path, r.Len) = r Then RetValue = Agent:URL 'We're blocked!
Else ' URL = NthField(URL, AllBots, 1) 'wildcard. we don't support complex patterns, just the *
If Left(path, URL.Len) = URL Then RetValue = Agent:URL 'We're blocked!
End If
If RetValue <> Nil Then Return True
Next
End If
Next
Next
Return True
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function Method(Method As String) As RequestMethod
Select Case Method
Case "GET"
Return RequestMethod.GET
Case "HEAD"
Return RequestMethod.HEAD
Case "DELETE"
Return RequestMethod.DELETE
Case "POST"
Return RequestMethod.POST
Case "PUT"
Return RequestMethod.PUT
Case "TRACE"
Return RequestMethod.TRACE
Case "OPTIONS"
Return RequestMethod.OPTIONS
Case "PATCH"
Return RequestMethod.PATCH
Case "CONNECT"
Return RequestMethod.CONNECT
Else
Return RequestMethod.InvalidMethod
End Select
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function MimeType(File As FolderItem) As String
Return MimeType(NthField(File.Name, ".", CountFields(File.Name, ".")))
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function MimeType(FileExtension As String) As String
Return MIMETypes.Lookup(FileExtension, "application/octet-stream")
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function SchemeToPort(Scheme As String) As Integer
Static mPorts As Dictionary
If mPorts = Nil Then
mPorts = New Dictionary( _
"http":80, _
"https":443, _
"ftp":21, _
"ssh":22, _
"telnet":23, _
"smtp":25, _
"smtps":25, _
"pop2":109, _
"pop3":110, _
"ident":113, _
"auth":113, _
"sftp":115, _
"nntp":119, _
"ntp":123, _
"irc":6667)
End If
Return mPorts.Lookup(Scheme, -1)
End Function
#tag EndMethod
#tag ComputedProperty, Flags = &h1
#tag Getter
Get
Static MIME As Dictionary
If MIME = Nil Then
MIME = New Dictionary( _
"http":"message/http", _
"ez":"application/andrew-inset", _
"aw":"application/applixware", _
"atom":"application/atom+xml", _
"atomcat":"application/atomcat+xml", _
"atomsvc":"application/atomsvc+xml", _
"ccxml":"application/ccxml+xml", _
"cdmia":"application/cdmi-capability", _
"cdmic":"application/cdmi-container", _
"cdmid":"application/cdmi-domain", _
"cdmio":"application/cdmi-object", _
"cdmiq":"application/cdmi-queue", _
"cu":"application/cu-seeme", _
"davmount":"application/davmount+xml", _
"daa":"application/x-daa", _
"dssc":"application/dssc+der", _
"xdssc":"application/dssc+xml", _
"ecma":"application/ecmascript", _
"emma":"application/emma+xml", _
"epub":"application/epub+zip", _
"exi":"application/exi", _
"pfr":"application/font-tdpfr", _
"stk":"application/hyperstudio", _
"ipfix":"application/ipfix", _
"jar":"application/java-archive", _
"ser":"application/java-serialized-object", _
"class":"application/java-vm", _
"js":"application/javascript", _
"json":"application/json", _
"lostxml":"application/lost+xml", _
"hqx":"application/mac-binhex40", _
"cpt":"application/mac-compactpro", _
"mads":"application/mads+xml", _
"mrc":"application/marc", _
"mrcx":"application/marcxml+xml", _
"ma":"application/mathematica", _
"nb":"application/mathematica", _
"mb":"application/mathematica", _
"mathml":"application/mathml+xml", _
"mbox":"application/mbox", _
"mscml":"application/mediaservercontrol+xml", _
"meta4":"application/metalink4+xml", _
"mets":"application/mets+xml", _
"mods":"application/mods+xml", _
"m21":"application/mp21", _
"mp21":"application/mp21", _
"mp4s":"application/mp4", _
"doc":"application/msword", _
"dot":"application/msword", _
"mxf":"application/mxf", _
"asc":"application/pgp-signature", _
"sig":"application/pgp-signature", _
"prf":"application/pics-rules", _
"p10":"application/pkcs10", _
"p7m":"application/pkcs7-mime", _
"p7c":"application/pkcs7-mime", _
"p7s":"application/pkcs7-signature", _
"p8":"application/pkcs8", _
"ac":"application/pkix-attr-cert", _
"cer":"application/pkix-cert", _
"crl":"application/pkix-crl", _
"pkipath":"application/pkix-pkipath", _
"pki":"application/pkixcmp", _
"pls":"application/pls+xml", _
"ai":"application/postscript", _
"eps":"application/postscript", _
"ps":"application/postscript", _
"cww":"application/prs.cww", _
"pskcxml":"application/pskc+xml", _
"rdf":"application/rdf+xml", _
"rif":"application/reginfo+xml", _
"rnc":"application/relax-ng-compact-syntax", _
"rl":"application/resource-lists+xml", _
"rld":"application/resource-lists-diff+xml", _
"rs":"application/rls-services+xml", _
"rsd":"application/rsd+xml", _
"rss":"application/rss+xml", _
"rtf":"application/rtf", _
"sbml":"application/sbml+xml", _
"scq":"application/scvp-cv-request", _
"scs":"application/scvp-cv-response", _
"spq":"application/scvp-vp-request", _
"spp":"application/scvp-vp-response", _
"sdp":"application/sdp", _
"setpay":"application/set-payment-initiation", _
"setreg":"application/set-registration-initiation", _
"shf":"application/shf+xml", _
"smi":"application/smil+xml", _
"smil":"application/smil+xml", _
"rq":"application/sparql-query", _
"srx":"application/sparql-results+xml", _
"gram":"application/srgs", _
"grxml":"application/srgs+xml", _
"sru":"application/sru+xml", _
"ssml":"application/ssml+xml", _
"tei":"application/tei+xml", _
"teicorpus":"application/tei+xml", _
"tfi":"application/thraud+xml", _
"tsd":"application/timestamped-data", _
"plb":"application/vnd.3gpp.pic-bw-large", _
"psb":"application/vnd.3gpp.pic-bw-small", _
"pvb":"application/vnd.3gpp.pic-bw-var", _
"tcap":"application/vnd.3gpp2.tcap", _
"pwn":"application/vnd.3m.post-it-notes", _
"aso":"application/vnd.accpac.simply.aso", _
"imp":"application/vnd.accpac.simply.imp", _
"acu":"application/vnd.acucobol", _
"atc":"application/vnd.acucorp", _
"acutc":"application/vnd.acucorp", _
"air":"application/vnd.adobe.air-application-installer-package+zip", _
"fxp":"application/vnd.adobe.fxp", _
"fxpl":"application/vnd.adobe.fxp", _
"xdp":"application/vnd.adobe.xdp+xml", _
"xfdf":"application/vnd.adobe.xfdf", _
"ahead":"application/vnd.ahead.space", _
"azf":"application/vnd.airzip.filesecure.azf", _
"azs":"application/vnd.airzip.filesecure.azs", _
"azw":"application/vnd.amazon.ebook", _
"acc":"application/vnd.americandynamics.acc", _
"ami":"application/vnd.amiga.ami", _
"apk":"application/vnd.android.package-archive", _
"cii":"application/vnd.anser-web-certificate-issue-initiation", _
"fti":"application/vnd.anser-web-funds-transfer-initiation", _
"atx":"application/vnd.antix.game-component", _
"mpkg":"application/vnd.apple.installer+xml", _
"m3u8":"application/vnd.apple.mpegurl", _
"swi":"application/vnd.aristanetworks.swi", _
"aep":"application/vnd.audiograph", _
"mpm":"application/vnd.blueice.multipass", _
"bmi":"application/vnd.bmi", _
"rep":"application/vnd.businessobjects", _
"cdxml":"application/vnd.chemdraw+xml", _
"mmd":"application/vnd.chipnuts.karaoke-mmd", _
"cdy":"application/vnd.cinderella", _
"cla":"application/vnd.claymore", _
"rp9":"application/vnd.cloanto.rp9", _
"c4g":"application/vnd.clonk.c4group", _
"c4d":"application/vnd.clonk.c4group", _
"c4f":"application/vnd.clonk.c4group", _
"c4p":"application/vnd.clonk.c4group", _
"c4u":"application/vnd.clonk.c4group", _
"c11amc":"application/vnd.cluetrust.cartomobile-config", _
"c11amz":"application/vnd.cluetrust.cartomobile-config-pkg", _
"csp":"application/vnd.commonspace", _
"cdbcmsg":"application/vnd.contact.cmsg", _
"cmc":"application/vnd.cosmocaller", _
"clkx":"application/vnd.crick.clicker", _
"clkk":"application/vnd.crick.clicker.keyboard", _
"clkp":"application/vnd.crick.clicker.palette", _
"clkt":"application/vnd.crick.clicker.template", _
"clkw":"application/vnd.crick.clicker.wordbank", _
"wbs":"application/vnd.criticaltools.wbs+xml", _
"pml":"application/vnd.ctc-posml", _
"ppd":"application/vnd.cups-ppd", _
"car":"application/vnd.curl.car", _
"pcurl":"application/vnd.curl.pcurl", _
"rdz":"application/vnd.data-vision.rdz", _
"uvf":"application/vnd.dece.data", _
"uvvf":"application/vnd.dece.data", _
"uvd":"application/vnd.dece.data", _
"uvvd":"application/vnd.dece.data", _
"uvt":"application/vnd.dece.ttml+xml", _
"uvvt":"application/vnd.dece.ttml+xml", _
"uvx":"application/vnd.dece.unspecified", _
"uvvx":"application/vnd.dece.unspecified", _
"fe_launch":"application/vnd.denovo.fcselayout-link", _
"dna":"application/vnd.dna", _
"mlp":"application/vnd.dolby.mlp", _
"dpg":"application/vnd.dpgraph", _
"dfac":"application/vnd.dreamfactory", _
"ait":"application/vnd.dvb.ait", _
"svc":"application/vnd.dvb.service", _
"geo":"application/vnd.dynageo", _
"mag":"application/vnd.ecowin.chart", _
"nml":"application/vnd.enliven", _
"esf":"application/vnd.epson.esf", _
"msf":"application/vnd.epson.msf", _
"qam":"application/vnd.epson.quickanime", _
"slt":"application/vnd.epson.salt", _
"ssf":"application/vnd.epson.ssf", _
"es3":"application/vnd.eszigno3+xml", _
"et3":"application/vnd.eszigno3+xml", _
"ez2":"application/vnd.ezpix-album", _
"ez3":"application/vnd.ezpix-package", _
"fdf":"application/vnd.fdf", _
"mseed":"application/vnd.fdsn.mseed", _
"seed":"application/vnd.fdsn.seed", _
"dataless":"application/vnd.fdsn.seed", _
"gph":"application/vnd.flographit", _
"ftc":"application/vnd.fluxtime.clip", _
"fm":"application/vnd.framemaker", _
"frame":"application/vnd.framemaker", _
"maker":"application/vnd.framemaker", _
"book":"application/vnd.framemaker", _
"fnc":"application/vnd.frogans.fnc", _
"ltf":"application/vnd.frogans.ltf", _
"fsc":"application/vnd.fsc.weblaunch", _
"oas":"application/vnd.fujitsu.oasys", _
"oa2":"application/vnd.fujitsu.oasys2", _
"oa3":"application/vnd.fujitsu.oasys3", _