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:

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2002 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

HierarchicalURI subclass:#HttpURI
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Net-Resources'
!

!HttpURI class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2002 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!HttpURI class methodsFor:'accessing'!

schemes

    ^ #(http)
! !

!HttpURI methodsFor:'defaults'!

defaultPort

    ^ 80
! !

!HttpURI methodsFor:'stream access'!

contents
    "use HTTPInterface for now"

    |response newURL|

    response := HTTPInterface get:self asString.
"/                    get:self path 
"/                    fromHost:self host 
"/                    port:self port
"/                    accept:#('*/*')  
"/                    fromDocument:nil.

    response isErrorResponse ifTrue:[ 
        Error raiseWith:response errorString:response responseText 
    ].
    response isMovedResponse ifTrue:[
        newURL := response location.
        newURL isNil ifTrue:[
            Error raiseWith:response errorString:response responseText. 
        ].
        ^ newURL asURL asURI contents.
    ].
    ^ response data

    "
     'http://www.puzzlers.org/pub/wordlists/unixdict.txt' asURI contents
     'http://www.exept.de/' asURI contents
    "

    "Modified (format): / 28-08-2018 / 09:01:16 / Claus Gittinger"
!

readStreamDo:aBlock
    "evaluate a block with the read stream as first argument
     and a dictionary containing attributes as second argument.
     The stream is closed after aBlock has been evaluated."

    "use HTTPInterface for now"

    |response headerInfo mime stream|

    response := HTTPInterface get:self asString. 
"/                    get:self path 
"/                    fromHost:self host 
"/                    port:self port
"/                    accept:#('*/*')  
"/                    fromDocument:nil.

    response isErrorResponse ifTrue:[
        ReadError 
            raiseRequestWith:response 
            errorString:('HTTP get of "%1" failed (%2)' bindWith:self asString with:response responseCode).
    ].

    headerInfo := response headerInfo.
    mime := headerInfo at:'content-type' ifAbsent:nil.
    mime notNil ifTrue:[
        headerInfo at:#MIME put:mime.
    ].
    ^ [
        stream := response dataStream.
        aBlock value:stream optionalArgument:headerInfo
    ] ensure:[
        stream close
    ].
    
    "
     'https://www.exept.de/' asURI readStreamDo:[:stream :attributes | 
         self halt
      ].
    "

    "Modified: / 18-03-2019 / 10:57:56 / Claus Gittinger"
    "Modified: / 22-10-2019 / 12:33:44 / Stefan Vogel"
! !

!HttpURI methodsFor:'testing'!

isAbsolute
    "there is nothing like a relative http URI"

    ^ true
!

isRemote
    "return true, if this is a remote URI"

    ^ true
! !

!HttpURI class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !