How do I set multiple cookies in a single Webrick response? - ruby

I use Webrick to test my HTTP client and I need to test how it gets and sets cookies.
Wikipedia provides an example of such response:
HTTP/1.0 200 OK
Content-type: text/html
Set-Cookie: theme=light
Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT
...
but if I do
server.mount_proc ?/ do |req, res|
res["set-cookie"] = %w{ 1=2 2=3 }
the whole array becomes a single cookie: "[\"1=2\", \"2=3\"]"
And then in WEBrick::HTTPResponse source code I see again the #header = Hash.new that probably means you can't repeat the header key.
Is it impossible?!
UPD:
This leaves me no hope:
https://github.com/rack/rack/issues/52#issuecomment-399629
https://github.com/rack/rack/blob/c859bbf7b53cb59df1837612a8c330dfb4147392/lib/rack/handler/webrick.rb#L98-L100

Another method should be used instead of res[...]=:
res.cookies.push WEBrick::Cookie.new("1", "2")
res.cookies.push WEBrick::Cookie.new("3", "4")
res.cookies.push WEBrick::Cookie.new("1", "5")

Related

Square Connect not paginating correctly for items

I'm trying to pull all the shop items from Square Connect API, and the pagination seems to be broken. Here is the request I'm making:
$ http https://connect.squareup.com/v1/{location id}/items 'Authorization: Bearer XXXXXXXXXXXX'
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Link
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Tue, 29 Nov 2016 09:25:50 GMT
ETag: "-307839789"
Keep-Alive: timeout=60
Strict-Transport-Security: max-age=631152000
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: DENY
X-Request-Id: ed/QSsnKQaeuhjoT4K/R
X-Response-Time: 529ms
X-XSS-Protection: 1; mode=block
transfer-encoding: chunked
[
...
]
Critically, the Link header is missing. The documentation here https://docs.connect.squareup.com/api/connect/v1/#pagination describes pagination, and it seems like the header should be present when there are more than 10 results (which there are).
Am I doing something wrong?
I think you are mixing up items and variations.
The response you get from the list items endpoints will give you each of your unique items (in your case ~140), each of those item objects will include additional information about their variations. If you expect to have more items you might actually be counting each variation as a different item.

Returning custom body with HTTP status error

I have a strange problem, I am testing on two different servers,
first server with a basic apache
second server with Zend Server
What I am trying to do it's to call an url on that server with cURL, this url can return one of the following codes: 200, 406 with a json body containing a message.
When querying the Apache server, on 406 error, I get this:
< HTTP/1.1 100 Continue
< HTTP/1.1 406 Not Acceptable
< Date: Wed, 10 Dec 2014 11:16:01 GMT
< Server: Apache/2.4.10 (Ubuntu)
< Content-Length: 75
< Content-Type: application/json
* HTTP error before end of send, stop sending
<
{"status":406,"message":"Domain not found (is the file named correctly ?)"}* Closing connection 0
And when querying the Zend Server, I get this:
< HTTP/1.1 100 Continue
< HTTP/1.1 406 Not Acceptable
< Date: Wed, 10 Dec 2014 11:15:05 GMT
< Connection: Keep-Alive
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 261
* HTTP error before end of send, stop sending
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>406 Not Acceptable</title>
</head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /webservice could not be found on this server.</p>
</body></html>
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
The header type is changed to text/html from application/json, and the body is a plain html error page.
Is this a Zend server issue ?
I dont think it is an issue with either server. The error code returned by both the servers is 406 but the error message in the http response body is sent as json in apache and html in zend server, hence the different content-types.
The two servers have chosen to send the error message in different formats hence the diffent content-types.

Print only specific headers using Curb gem [duplicate]

This question already has an answer here:
Get response headers from Curb
(1 answer)
Closed 8 years ago.
I have a question about Ruby gem - Curb. I'm playing around with this gem and have this piece of code:
require 'curb'
require 'colorize'
def err(msg)
puts
puts msg.red
puts 'HOWTO: '.white + './script.rb <domain>'.red
puts
end
target = ARGV[0] || err("You forgot something....")
Curl::Easy.perform(target) do |curl|
curl.headers["User-Agent"] = "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.7.7) Gecko/20050421"
curl.verbose = true
end
For example, when I try it on google.com, I get this headers (I don't put whole results from script):
Host: google.com
Accept: */*
User-Agent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.7.7) Gecko/20050421
* STATE: DO => DO_DONE handle 0x1c8dd80; (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x1c8dd80; (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x1c8dd80; (connection #0)
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Location: https://www.google.cz/?gfe_rd=cr&ei=2stTVO2eJumg8we6woGoCg
< Content-Length: 259
< Date: Fri, 31 Oct 2014 17:50:18 GMT
< Server: GFE/2.0
< Alternate-Protocol: 443:quic,p=0.01
My question, Is there any way, how to print only a specific headers via Curb? For example, I'd like only this headers on output, like this:
Content-Type: text/html; charset=UTF-8
Location: https://www.google.cz/?gfe_rd=cr&ei=2stTVO2eJumg8we6woGoCg
Server: GFE/2.0
And nothing anymore. Is there any how to to do it via this gem? Or if you have any ideas how to do it using some another gem, let me know.
It's not the most difficult thing to just parse it yourself.
That's exactly what "Get response headers from Curb" proposes.

How do I view the response cookies from a HTTParty get request?

I'm using httparty to do a GET request to a certain website. I need the response cookies in order to successfully make a POST request to login.
rest_client makes it really easy, all I have to do is:
get_request = RestClient.get('<REDACTED>')
response_cookies = get_request.cookies
# =>
{
"sessiontype"=>"mpb",
"aac"=>"741F9EC20A4C422369F7564445611591",
"Expires"=>"Sun",
"Path"=>"%2F",
"Domain"=>"<REDACTED>",
"internetbankierenmi"=>"1559079104.20480.0000",
"TSdb640d"=>"d17ca2538ee2215b647c3466d4b06da7ec33c7a21dc7217953d3ffe7d4efbe89959deba9debace3f579e71c9e27e0b6b1ea2c663"
}
But I want to do the same with httparty. So my question is how do I view/access the response cookies from a GET request in httparty?
Since cookies are part of the response header, you can access them from the response header in httparty.
require 'httparty'
r = HTTParty.get('<REDACTED>')
r.headers
# => {"date"=>["Sat, 26 Jul 2014 19:34:09 GMT"], "cache-control"=>["no-cache", "no-store"], "pragma"=>["no-cache"], "expires"=>["Thu, 01 Jan 1970 00:00:00 GMT"], "content-length"=>["13987"], "set-cookie"=>["sessiontype=mpb; Secure", "aac=742A83A50A4C422C24D6F952C4BF6355; Expires=Sun, 26 Jul 2015 19:34:08 GMT; Path=/; Domain=<REDACTED>; Secure", "internetbankierenmi=1777182912.20480.0000; path=/", "TSdb640d=de34831032c17e8b66f123633372a9b341a9773368fccca553d402b1d4efbe89959deba9debace3fe0d47c86e27e0b6b1669bf05; Path=/"], "vary"=>["Accept-Encoding,User-Agent"], "content-type"=>["text/html;charset=ISO-8859-1"], "content-language"=>["en"], "connection"=>["close"], "strict-transport-security"=>["max-age=8640000"]}
r.headers['set-cookie']
# => "sessiontype=mpb; Secure, aac=742A83A50A4C422C24D6F952C4BF6355; Expires=Sun, 26 Jul 2015 19:34:08 GMT; Path=/; Domain=.ing.nl; Secure, internetbankierenmi=1777182912.20480.0000; path=/, TSdb640d=de34831032c17e8b66f123633372a9b341a9773368fccca553d402b1d4efbe89959deba9debace3fe0d47c86e27e0b6b1669bf05; Path=/"

Why I'm getting a 500 server response in Google Webmaster Tools?

I'm trying to figure out why my web isn't getting crawled by the Google Spider, and when I do a test about the response of the site I see that I'm getting a 500 server response BUT my website is actually working.
My site is http://lacasadelilihostal.com/ and was made in Laravel.
I see another question when said that the problem could be by a JavaScript error but I don't see anyone in my web; and also I changed the permissions of the public folder to 775 but didn't work either.
This is the exact header response of your homepage. I'm taking a little time to digest this information and will come back. In the meantime others can consider this info too:
HTTP/1.1 200 OK
Date: Thu, 15 Aug 2013 17:35:33 GMT
Server: Apache
X-Powered-By: PHP/5.3.26
Cache-Control: no-cache
Set-Cookie: laravel_session=76c6f66056ff1828be5ad677b87f9690; expires=Thu, 15-Aug-2013 19:35:33 GMT; path=/; HttpOnly
Set-Cookie: laravel_session=76c6f66056ff1828be5ad677b87f9690; expires=Thu, 15-Aug-2013 19:35:34 GMT; path=/; httponly
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
I check my logs and found that i'm getting the error "'Uninitialized string offset: " in this line:
`$langcode = (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$langcode = (!empty($langcode)) ? explode(";", $langcode) : $langcode;
$langcode = (!empty($langcode['0'])) ? explode(",", $langcode['0']) : $langcode;
$langcode = (!empty($langcode['0'])) ? explode("-", $langcode['0']) : $langcode;
$langcode = strtolower($langcode[0]);` `
This only happened with crawlers... I remove the last line.

Resources