HttpURI.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5226 4447f103d1ff
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
     1
"{ Encoding: utf8 }"
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
     2
1309
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     3
"
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     4
 COPYRIGHT (c) 2002 by eXept Software AG
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     5
              All Rights Reserved
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     6
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     7
 This software is furnished under a license and may be used
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     8
 only in accordance with the terms of that license and with the
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    10
 be provided or otherwise made available to, or used by, any
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    11
 other person.  No title to or ownership of the software is
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    12
 hereby transferred.
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    13
"
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    16
"{ NameSpace: Smalltalk }"
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    17
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
HierarchicalURI subclass:#HttpURI
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
	instanceVariableNames:''
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
	classVariableNames:''
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
1310
948ed141b0b7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1309
diff changeset
    22
	category:'Net-Resources'
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
1309
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    25
!HttpURI class methodsFor:'documentation'!
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    26
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    27
copyright
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    28
"
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    29
 COPYRIGHT (c) 2002 by eXept Software AG
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    30
              All Rights Reserved
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    31
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    32
 This software is furnished under a license and may be used
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    33
 only in accordance with the terms of that license and with the
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    35
 be provided or otherwise made available to, or used by, any
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    36
 other person.  No title to or ownership of the software is
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    37
 hereby transferred.
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    38
"
c752d54f4e09 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1276
diff changeset
    39
! !
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    40
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
!HttpURI class methodsFor:'accessing'!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    43
schemes
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    44
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    45
    ^ #(http)
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    46
! !
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    47
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    48
!HttpURI methodsFor:'defaults'!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    49
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    50
defaultPort
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    51
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    52
    ^ 80
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    53
! !
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    54
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    55
!HttpURI methodsFor:'stream access'!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    56
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    57
contents
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    58
    "use HTTPInterface for now"
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    59
5220
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    60
    |response newURL|
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    61
5220
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    62
    response := HTTPInterface get:self asString.
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    63
"/                    get:self path 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    64
"/                    fromHost:self host 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    65
"/                    port:self port
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    66
"/                    accept:#('*/*')  
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    67
"/                    fromDocument:nil.
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    68
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    69
    response isErrorResponse ifTrue:[ 
5220
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    70
        Error raiseWith:response errorString:response responseText 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    71
    ].
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    72
    response isMovedResponse ifTrue:[
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    73
        newURL := response location.
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    74
        newURL isNil ifTrue:[
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    75
            Error raiseWith:response errorString:response responseText. 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    76
        ].
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    77
        ^ newURL asURL asURI contents.
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    78
    ].
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    79
    ^ response data
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    80
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    81
    "
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    82
     'http://www.puzzlers.org/pub/wordlists/unixdict.txt' asURI contents
5220
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    83
     'http://www.exept.de/' asURI contents
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    84
    "
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    85
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    86
    "Modified (format): / 28-08-2018 / 09:01:16 / Claus Gittinger"
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    87
!
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    88
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    89
readStreamDo:aBlock
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    90
    "evaluate a block with the read stream as first argument
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    91
     and a dictionary containing attributes as second argument.
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    92
     The stream is closed after aBlock has been evaluated."
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    93
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    94
    "use HTTPInterface for now"
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    95
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    96
    |response headerInfo mime stream|
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
5220
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    98
    response := HTTPInterface get:self asString. 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
    99
"/                    get:self path 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
   100
"/                    fromHost:self host 
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
   101
"/                    port:self port
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
   102
"/                    accept:#('*/*')  
e6d2f9682889 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 4879
diff changeset
   103
"/                    fromDocument:nil.
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   104
5226
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   105
    response isErrorResponse ifTrue:[
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   106
        ReadError 
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   107
            raiseRequestWith:response 
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   108
            errorString:('HTTP get of "%1" failed (%2)' bindWith:self asString with:response responseCode).
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   109
    ].
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   110
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
    headerInfo := response headerInfo.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
    mime := headerInfo at:'content-type' ifAbsent:nil.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   113
    mime notNil ifTrue:[
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   114
        headerInfo at:#MIME put:mime.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   115
    ].
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   116
    ^ [
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   117
        stream := response dataStream.
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   118
        aBlock value:stream optionalArgument:headerInfo
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   119
    ] ensure:[
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   120
        stream close
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   121
    ].
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   122
    
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   123
    "
5225
a4c3c856b3c8 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 5220
diff changeset
   124
     'https://www.exept.de/' asURI readStreamDo:[:stream :attributes | 
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   125
         self halt
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   126
      ].
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   127
    "
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   128
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   129
    "Modified: / 18-03-2019 / 10:57:56 / Claus Gittinger"
5226
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   130
    "Modified: / 22-10-2019 / 12:33:44 / Stefan Vogel"
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   131
! !
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   132
1276
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   133
!HttpURI methodsFor:'testing'!
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   134
1461
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   135
isAbsolute
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   136
    "there is nothing like a relative http URI"
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   137
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   138
    ^ true
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   139
!
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   140
1276
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   141
isRemote
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   142
    "return true, if this is a remote URI"
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   143
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   144
    ^ true
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   145
! !
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   146
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   147
!HttpURI class methodsFor:'documentation'!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   148
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   149
version
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
   150
    ^ '$Header$'
5226
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   151
!
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   152
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   153
version_CVS
4447f103d1ff #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5225
diff changeset
   154
    ^ '$Header$'
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
! !
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
   156