Discussion:
How to capture HTTP response with the status 500?
Al Le
2017-01-14 00:11:12 UTC
Permalink
Hello,

the GET task allows to save the response of a HTTP server. One URL I try
to get returns the HTTP status 500 (internal server error). Along with
the status, some data is returned in the response (I verified this with
cUrl) which provides some details about the error. However this data is
not captured by ant.

Is it possible (and how) to capture the contents of a server response
with the status 500?

After the data has been captured (saved to file), the execution of the
build should proceed as usual, i.e. go further of fail according to
"ignoreerrors".

Thanks!
AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Jan Matèrne (jhm)
2017-01-16 07:27:42 UTC
Permalink
I just had a look at the source code.
There is a special handling for HTTP-ResponseCodes for
- MOVED --> follow redirect if specified
-- 301: Moved Permanently
-- 302: Temporary Redirect
-- 303: See Other
-- 307: Temporary Redirect
- HTTP_NOT_MODIFIED (304 Not Modified) --> dont download if use timestamp
- HTTP_UNAUTHORIZED (401: Unauthorized) --> maybe throw BuildException, dont download

Otherwise the return of the url.openConnection().getInputStream() is copied to local file.

So a response for a 500-error should be captured to file.
_But_ that file is deleted if an error (non-success, e.g. catched IOException) occured.
(org.apache.tools.ant.taskdefs.Get:887).

For a workaround you could
- get the <get> source code
- modify the deletion logic
- compile that code
- include your modified code via <taskdef name="get2" classname="..." classpath="..."/>
- try it out
(- provide a patch ;)


Maybe we could also tune that task.
- support of HTTP 308 Permanent Redirect
- store the HTTP-returncode in a map (URL-RC) for access via property ${get-rc.http://ant.apache.org}==200
- parallel downloads?
- different "delete" logic as specified via new attribute
-- delete (old behaviour)
-- move to different folder
-- move to different name/folder (e.g. get-error/http-ant.apache.org--207_MULTI_STATUS)


Jan





Jan





> -----Ursprüngliche Nachricht-----
> Von: Al Le [mailto:***@gmx.de]
> Gesendet: Samstag, 14. Januar 2017 01:11
> An: Ant Users List
> Betreff: How to capture HTTP response with the status 500?
>
> Hello,
>
> the GET task allows to save the response of a HTTP server. One URL I
> try to get returns the HTTP status 500 (internal server error). Along
> with the status, some data is returned in the response (I verified this
> with
> cUrl) which provides some details about the error. However this data is
> not captured by ant.
>
> Is it possible (and how) to capture the contents of a server response
> with the status 500?
>
> After the data has been captured (saved to file), the execution of the
> build should proceed as usual, i.e. go further of fail according to
> "ignoreerrors".
>
> Thanks!
> AL
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-***@ant.apache.org For additional
> commands, e-mail: user-***@ant.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Al Le
2017-01-16 19:50:26 UTC
Permalink
Jan, thank you for taking time!

Am 16.01.2017 um 08:27 schrieb Jan Matèrne (jhm):
>
> Otherwise the return of the url.openConnection().getInputStream() is copied to local file.
>
> So a response for a 500-error should be captured to file.
> _But_ that file is deleted if an error (non-success, e.g. catched IOException) occured.
> (org.apache.tools.ant.taskdefs.Get:887).

IIRC, it didn't get to downloading the file. Instead, it stopped at
"connection.getInputStream()" (org.apache.tools.ant.taskdefs.Get:804). I
remember three messages in the ant log stating "Error opening connection".

So maybe it's because of the inbuilt JDK's implementation of
connection.getInputStream (it throws an exception if the status is 500).

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Al Le
2017-01-17 07:08:41 UTC
Permalink
> So maybe it's because of the inbuilt JDK's implementation of
> connection.getInputStream (it throws an exception if the status is 500).

I've debugged it and found out that
the connection object is of type 'sun.net.www.protocol.https.HttpsURLConnectionImpl'
which delegates the getInputStream() method to 'sun.net.www.protocol.http.HttpURLConnection'
which throws an exception if the status is greater than or equal to 400.

This makes me think that it will not be possible to get the full response without a custom implementation of URLConnection.

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Klaus Malorny
2017-01-17 08:17:04 UTC
Permalink
On 17.01.17 08:08, Al Le wrote:
>> So maybe it's because of the inbuilt JDK's implementation of
>> connection.getInputStream (it throws an exception if the status is 500).
>
> I've debugged it and found out that
> the connection object is of type 'sun.net.www.protocol.https.HttpsURLConnectionImpl'
> which delegates the getInputStream() method to 'sun.net.www.protocol.http.HttpURLConnection'
> which throws an exception if the status is greater than or equal to 400.
>
> This makes me think that it will not be possible to get the full response without a custom implementation of URLConnection.
>
> AL
>


Hi AL,

I don't think you'll need to have a custom implementation. I cannot remember
whether I have ever used it, but I am quite confident that you can call the
Method HttpURLConnection.getErrorStream within the respective catch block to
read the body of the HTTP response. Hope that helps.

Regards,

Klaus

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Al Le
2017-01-17 12:05:03 UTC
Permalink
Hello Klaus,

> I am quite confident that you can call the
> Method HttpURLConnection.getErrorStream within the respective catch block to
> read the body of the HTTP response. Hope that helps.

Thank you for the tip! I didn't know of that method!

Indeed, with getErrorStream() I could read out the response (had to downcast to HttpURLConnection first of course).

IMO it would be a very useful enhancement to the Get task if one could

1. Specify the property to write the response code (status code) to

2. Specify the file to write the error response to (or if the task just wrote it to the file specified by 'dest'). If 'ignoreerrors' is set to false, the build should fail as usual.

With these two enhancements, the script could set 'ignoreerrors' to 'true' and then analyze what happened and proceed as it fits.

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Loading...