MIgrating pier link checker to Pharo 3

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

MIgrating pier link checker to Pharo 3

Stephan Eggermont-3
I’m trying to migrate the Pier link checker to Pharo 3, going from
HTTPSocket to ZnClient. Looks like a good API improvement, but some things
are still different. http://humane-assessment.com claims not to  support
this head request (501), as does http:://cognitive-edge.com (404).
Anyone know what I should change?

Stephan

httpHead: aString
        "Answer an array with response code and effective URL, or nil."

        | response |
                response := ZnClient new
                url: aString trimBoth;
                timeout: 1;
                method: #HEAD;
                ifFail: [ ^Array with: 404 with: aString ];
                executeWithTimeout;
                response.
        ^ Array with: response code with: (response headers at: 'Location' ifAbsent: [aString]) .

httpHead: aString
        "Answer an array with response code and effective URL, or nil."

        | url socket |
        url := Url absoluteFromText: aString.
        (url isKindOf: HttpUrl)
                ifFalse: [ ^ nil ].
        [ socket := HTTPSocket
                initHTTPSocket: url
                wait: (HTTPSocket deadlineSecs: 1)
                ifError: [ ^ Array with: 404 with: aString ] ]
                        on: NameLookupFailure
                        do: [ :err | ^ Array with: 404 with: aString ].
        ^ [ [ socket sendCommand: ('HEAD ' , ((HTTPSocket shouldUseProxy: url authority)
                ifTrue: [ url printString ] ifFalse: [ url fullPath ]) ,  ' HTTP/1.0' , String crlf) ,
                        ('Host: ' , url authority , String crlf) ,
                        (HTTPSocket userAgentString , String crlf) ,
                        (HTTPSocket classPool at: #HTTPProxyCredentials).
                socket header: (socket getResponseUpTo: String crlf , String crlf) first.
                Array with: socket responseCode asInteger with: (socket getHeader: 'location' default: aString) ]
                        on: Error do: [ :err | nil ] ]
                        ensure: [ socket destroy ]
Reply | Threaded
Open this post in threaded view
|

Re: MIgrating pier link checker to Pharo 3

Sven Van Caekenberghe-2
Stephan,

I am not sure what you are trying to achieve with HEAD and Location, I have never seen that. Are you trying to resolve redirects ? There should be no need for that since Zn follows them by default.

But to answer your questions, this code does work:

ZnClient new
  url: 'http://cognitive-edge.com';
  timeout: 5;
  method: #HEAD;
  execute;
  response.

A timeout of 1 second was too short. Also #executeWithTimeout is internal API, you should use #execute. There was also a typo in your URL (2 colons).

For the other URL, the code also works:

ZnClient new
  url: 'http://humane-assessment.com/';
  timeout: 1;
  method: #HEAD;
  execute;
  response.

However this site simply refuses to service HEAD requests, which is a valid response. You can see the same with curl:

$ curl -X HEAD -D - http://humane-assessment.com/
HTTP/1.1 501 Not Implemented
Date: Wed, 05 Mar 2014 13:07:48 GMT
Server: Zinc HTTP Components 1.0
Content-Type: text/plain
Content-Length: 512
Via: 1.1 humane-assessment.com
Vary: Accept-Encoding
Connection: close

Note that you can also send #location to a ZnResponse instance. If you don't have to set the timeout, the following is even shorter:

ZnEasy head: 'http://cognitive-edge.com'.

HTH,

Sven

On 05 Mar 2014, at 13:58, Stephan Eggermont <[hidden email]> wrote:

> I’m trying to migrate the Pier link checker to Pharo 3, going from
> HTTPSocket to ZnClient. Looks like a good API improvement, but some things
> are still different. http://humane-assessment.com claims not to  support
> this head request (501), as does http:://cognitive-edge.com (404).
> Anyone know what I should change?
>
> Stephan
>
> httpHead: aString
> "Answer an array with response code and effective URL, or nil."
>
> | response |
> response := ZnClient new
> url: aString trimBoth;
> timeout: 1;
> method: #HEAD;
> ifFail: [ ^Array with: 404 with: aString ];
> executeWithTimeout;
> response.
> ^ Array with: response code with: (response headers at: 'Location' ifAbsent: [aString]) .
>
> httpHead: aString
> "Answer an array with response code and effective URL, or nil."
>
> | url socket |
> url := Url absoluteFromText: aString.
> (url isKindOf: HttpUrl)
> ifFalse: [ ^ nil ].
> [ socket := HTTPSocket
> initHTTPSocket: url
> wait: (HTTPSocket deadlineSecs: 1)
> ifError: [ ^ Array with: 404 with: aString ] ]
> on: NameLookupFailure
> do: [ :err | ^ Array with: 404 with: aString ].
> ^ [ [ socket sendCommand: ('HEAD ' , ((HTTPSocket shouldUseProxy: url authority)
> ifTrue: [ url printString ] ifFalse: [ url fullPath ]) ,  ' HTTP/1.0' , String crlf) ,
> ('Host: ' , url authority , String crlf) ,
> (HTTPSocket userAgentString , String crlf) ,
> (HTTPSocket classPool at: #HTTPProxyCredentials).
> socket header: (socket getResponseUpTo: String crlf , String crlf) first.
> Array with: socket responseCode asInteger with: (socket getHeader: 'location' default: aString) ]
> on: Error do: [ :err | nil ] ]
> ensure: [ socket destroy ]


Reply | Threaded
Open this post in threaded view
|

Re: MIgrating pier link checker to Pharo 3

Stephan Eggermont-3
In reply to this post by Stephan Eggermont-3
Sven wrote:
>I am not sure what you are trying to achieve with HEAD and Location, I have never seen that. Are you trying to resolve >redirects ? There should be no need for that since Zn follows them by default.

Thanks.

It is called Pier-LinkChecker. It is supposed to check all links in a Pier site and create a report
of the actual location links redirect to and error responses & timeouts. I’m just trying to make it work
in Pharo 3.

Stephan