SWI-Prolog / packages-http

The SWI-Prolog HTTP server and client libraries
23 stars 23 forks source link

Accept status codes 201 thru 299 as successful #119

Closed Anniepoo closed 5 years ago

Anniepoo commented 5 years ago

Current behavior of http_open without an explicit status_code option is to throw an existance error for status codes 201 through 299. This is explicitly incorrect for 201 (created), 202 (accepted), 203 (non-authoritative), 204 (no content), 205(reset content), and 206 (partial content). It is expected behavior for all other 201-299 codes.

RFC2616

This PR causes http_open to treat status codes 201-299 as equivalent to 200 when there is no explicit status_code option.

Behavior of HEAD ?- http_open:http_open('https://httpstat.us/200', Stream, []), open_null_stream(Null), copy_stream_data(Stream, Null). Stream = (0x558ddeb39e10,0x558ddeb39f20), Null = (0x558ddeb3a030).

?- http_open:http_open('https://httpstat.us/201', Stream, []), open_null_stream(Null), copy_stream_data(Stream, Null). ERROR: url `'https://httpstat.us/201'' does not exist (status(201,Created)) ERROR: In: ERROR: [12] throw(error(existence_error(url,'https://httpstat.us/201'),context(_686,...))) ERROR: [10] http_open:try_http_proxy(direct,'','','') at /home/anniepoo/.swivm/versions/7.7.18/lib/swipl-7.7.18/library/http/http_open.pl:418 ERROR: [8] ''('') ERROR: [7] ERROR: ERROR: Note: some frames are missing due to last-call optimization. ERROR: Re-run your program in debug mode (:- debug.) to get more detail. ?-

following change

?- http_open:http_open('https://httpstat.us/201', Stream, []), open_null_stream(Null), copy_stream_data(Stream, Null). Stream = (0x558dde9d7a50,0x558ddea0f1e0), Null = (0x558dde73f050).

?-

?- http_open:http_open('https://httpstat.us/201', Stream, [status_code(Code)]), opennull Stream = (0x558dde6bf040,0x558ddea09620), Code = 201, Null = (0x558dde6b33a0).

JanWielemaker commented 5 years ago

Thanks. Merged after discussion and edits.