HttpURI.st
author Claus Gittinger <cg@exept.de>
Sat, 04 May 2019 12:32:23 +0200
changeset 4940 d5d7391bc875
parent 4879 d6b8308d7c51
child 5220 e6d2f9682889
permissions -rw-r--r--
#FEATURE by cg class: SharedCollection added: #accessLock
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
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    60
    |response|
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    61
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    62
    response := HTTPInterface get:self path 
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    63
                    fromHost:self host 
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    64
                    port:self port
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    65
                    accept:#('*/*')  
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    66
                    fromDocument:nil.
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    67
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    68
    response isErrorResponse ifTrue:[ 
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    69
        self error: response responseText 
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    70
    ].
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    71
    ^ response data
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    72
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    73
    "
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    74
     'http://www.puzzlers.org/pub/wordlists/unixdict.txt' asURI contents
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    75
    "
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    76
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
    77
    "Modified (format): / 28-08-2018 / 09:01:16 / Claus Gittinger"
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    78
!
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
    79
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    80
readStreamDo:aBlock
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    81
    "evaluate a block with the read stream as first argument
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    82
     and a dictionary containing attributes as second argument.
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    83
     The stream is closed after aBlock has been evaluated."
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    84
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    85
    "use HTTPInterface for now"
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    86
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    87
    |response headerInfo mime stream|
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    88
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    89
    response := HTTPInterface 
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
    90
                    get:self path 
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    91
                    fromHost:self host 
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    92
                    port:self port
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    93
                    accept:#('*/*')  
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    94
                    fromDocument:nil.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    95
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    96
    headerInfo := response headerInfo.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
    mime := headerInfo at:'content-type' ifAbsent:nil.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    98
    mime notNil ifTrue:[
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    99
        headerInfo at:#MIME put:mime.
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   100
    ].
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   101
    ^ [
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   102
        stream := response dataStream.
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   103
        aBlock value:stream optionalArgument:headerInfo
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   104
    ] ensure:[
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   105
        stream close
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   106
    ].
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   107
    
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
    "
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   109
     'http://www.exept.de/' asURI readStreamDo:[:stream :attributes | 
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   110
         self halt
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
      ].
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
    "
4879
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   113
d6b8308d7c51 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4722
diff changeset
   114
    "Modified: / 18-03-2019 / 10:57:56 / Claus Gittinger"
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   115
! !
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   116
1276
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   117
!HttpURI methodsFor:'testing'!
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   118
1461
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   119
isAbsolute
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   120
    "there is nothing like a relative http URI"
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   121
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   122
    ^ true
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   123
!
998d6076f34d Fix absolute UR handling
Stefan Vogel <sv@exept.de>
parents: 1310
diff changeset
   124
1276
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   125
isRemote
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   126
    "return true, if this is a remote URI"
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   127
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   128
    ^ true
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   129
! !
fd3f0d37513c Print without password.
Stefan Vogel <sv@exept.de>
parents: 1005
diff changeset
   130
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   131
!HttpURI class methodsFor:'documentation'!
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   132
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   133
version
4722
fe159f4a0668 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 2870
diff changeset
   134
    ^ '$Header$'
1005
7ed6fa7ccfba initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   135
! !
2870
6eef483653e5 class: HttpURI
Claus Gittinger <cg@exept.de>
parents: 1461
diff changeset
   136