Print only specific headers using Curb gem [duplicate] - ruby

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.

Related

karate framework graphql error on standalone [duplicate]

This question already has an answer here:
Unable to use read('classpath:') when running tests with standalone karate.jar
(1 answer)
Closed 1 year ago.
I have a simple graphql which works well in the maven build but getting error when executed as a feature file with the standalone karate jar.
Here is the graphql used in request
getCustomerById.graphqls
-----------------------
query{
getCustomerById(custid: "12345"){
custid
firstname
lastname
address1_text
address2_text
city_text
state_text
zip_text
}
}
-------------------------
#Feature file
graphql.feature
* configure ssl = { keyStore: 'classpath:customer/test.pfx', keyStorePassword: 'test123', keyStoreType: 'pkcs12' }
Given url 'https://<IT_URL>/graphql-data/v1/graphql'
* def customerRequest = read('getCustomerById.graphqls')
And def variables = { customerid: '123456'}
And request { query: '#(query)', variables: '#(variables)' }
When method post
Then status 200
* print 'Response ==>', response
getting the following error
======
18:31:42.699 [main] WARN com.intuit.karate.JsonUtils - object to json serialization failure, trying alternate approach: [B cannot be cast to [Ljava.lang.Object;
18:31:42.701 [main] DEBUG com.intuit.karate - request:
2 > POST https://<ITURL>/graphql-data/v1/graphql
2 > Content-Type: application/json; charset=UTF-8
2 > Content-Length: 62
2 > Host: it-xxx-dns.com
2 > Connection: Keep-Alive
2 > User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_291)
2 > Accept-Encoding: gzip,deflate
{"variables":{"customerId":"792798178595168"},"query":"[B#7ca8d498"}
18:31:43.060 [main] DEBUG com.intuit.karate - response time in milliseconds: 357
2 < 200
2 < Date: Wed, 30 Jun 2021 23:31:42 GMT
2 < Content-Type: application/json;charset=UTF-8
2 < Content-Length: 109
2 < Connection: keep-alive
2 < Access-Control-Allow-Origin: *
2 < Access-Control-Allow-Methods: *
2 < Access-Control-Max-Age: 3600
2 < Access-Control-Allow-Headers: authorization, content-type, xsrf-token
2 < Access-Control-Expose-Headers: xsrf-token
2 < Vary: Origin
2 < Vary: Access-Control-Request-Method
2 < Vary: Access-Control-Request-Headers
2 < Strict-Transport-Security: max-age=15724800; includeSubDomains
2 < Set-Cookie: INGRESSCOOKIE=1625095903.954.332.448531; Domain=it-i3-xxx-dns.com; Secure
{"errors":[{"description":"Invalid Syntax : offending token '[' at line 1 column 1","error_code":"400-900"}]}
18:31:43.061 [main] INFO com.intuit.karate - [print] Response ==> {
"errors": [
{
"description": "Invalid Syntax : offending token '[' at line 1 column 1",
"error_code": "400-900"
}
]
}
======
Can you please let me know what's wrong with the code. Is it because of the SSL and passing the pfx file it behaves differently in the standalone jar . I'm not able to find out but it works perfectly fine in the maven build
Yes in stand-alone mode it is preferred you use file: instead of classpath: unless you know how to properly set the class-path.
Please read this for more info and try to figure this out: https://stackoverflow.com/a/58398958/143475

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

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")

PHRETS:GetMetadataTypes() called but unable to find GetMetadata location

I'm building a RETS based application and every time I try to run the script it shows "GetMetadataTypes() called but unable to find GetMetadata location. Failed login?". I'm not sure what exactly is causing this issue. Below is my code snippet for your review:
if ($connect)
{
echo " + Connected<br>\n";
$types = $rets->GetMetadataTypes();
// check for errors
if (!$types)
{
print_r($rets->Error());
}
else
{
var_dump($types);
}
}
Below is the debug log for your review.
* About to connect() to ctarmls2.apps.retsiq.com port 80 (#0)
* Trying 107.22.214.38...
* connected
* Connected to ctarmls2.apps.retsiq.com (107.22.214.38) port 80 (#0)
GET /acc/rets/login HTTP/1.1
Host: ctarmls2.apps.retsiq.com
Accept: /
RETS-Version: RETS/1.5
User-Agent: PHRETS/1.0
Accept: /
< HTTP/1.1 401 Unauthorized
< Cache-Control: private
< Cache-Control: private
< Content-Type: text/html;charset=utf-8
< Date: Thu, 18 Dec 2014 09:00:26 GMT
< MIME-Version: 1.0
< RETS-Version: RETS/1.5
< Server: nginx/1.0.11
Added cookie JSESSIONID="94616DF90574A5747A5CC58526968DC4" for domain ctarmls2.apps.retsiq.com, path /acc, expire 0
< Set-Cookie: JSESSIONID=94616DF90574A5747A5CC58526968DC4; Path=/acc
Replaced cookie JSESSIONID="94616DF90574A5747A5CC58526968DC4" for domain ctarmls2.apps.retsiq.com, path /acc, expire 0
< Set-Cookie: JSESSIONID=94616DF90574A5747A5CC58526968DC4; Path=/acc
< WWW-Authenticate: Digest realm="rets#flexmls.com", qop="auth", nonce="055a98f2718db640bb49b25727b265c7", opaque="e740e530f881b719ac847f225d70ef26"
< Content-Length: 954
< Connection: keep-alive
<
Ignoring the response-body
Connection #0 to host ctarmls2.apps.retsiq.com left intact
Issue another request to this URL: 'http://ctarmls2.apps.retsiq.com:80/acc/rets/login'
Re-using existing connection! (#0) with host (nil)
Connected to (nil) (107.22.214.38) port 80 (#0)
Server auth using Digest with user 'chs.rets.asolz1-i33'
GET /acc/rets/login HTTP/1.1
Authorization: Digest username="chs.rets.asolz1-i33", realm="rets#flexmls.com", nonce="055a98f2718db640bb49b25727b265c7", uri="/acc/rets/login", cnonce="MTUzODg1", nc=00000001, qop="auth", response="a28c686f4af4100dc6f417c013c44233", opaque="e740e530f881b719ac847f225d70ef26"
Host: ctarmls2.apps.retsiq.com
Accept: /
Cookie: JSESSIONID=94616DF90574A5747A5CC58526968DC4
RETS-Version: RETS/1.5
User-Agent: PHRETS/1.0
Accept: /
< HTTP/1.1 200 OK
< Cache-Control: private
< Cache-Control: private
< Content-Type: text/xml
< Date: Thu, 18 Dec 2014 09:00:26 GMT
< MIME-Version: 1.0
< RETS-Version: RETS/1.5
< Server: nginx/1.0.11
Replaced cookie JSESSIONID="94616DF90574A5747A5CC58526968DC4" for domain ctarmls2.apps.retsiq.com, path /acc, expire 0
< Set-Cookie: JSESSIONID=94616DF90574A5747A5CC58526968DC4; Path=/acc
< WWW-Authenticate: Digest realm="rets#retsiq.com",qop="auth",nonce="1647953c3586fee2f03a44259066e02d", opaque="31b02b3042ea6"
< Content-Length: 283
< Connection: keep-alive
<
Connection #0 to host (nil) left intact
MemberName=chs.rets.asolz1-i33
User=chs.rets.asolz1-i33,1,MEMBER,20141216152304900080000000
Broker=RETS_OFFIC
MetadataVersion=01.01.71275
MinMetadataVersion=01.01.71275
Logout=/acc/rets/logout
Closing connection #0
Thanks
It is the rets server issue. The server has to specify a metadata url location which is displaying in the xml response when logging via a browser which is missing for this server. Tell this concern to rets server maintenance team.

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.

How to disable READ_XBUF caching?

I am using this simple code.
#include "gwan.h"
int main(int argc, char *argv[])
{
xbuf_t *reply = get_reply(argv), *read_buff;
read_buff = (xbuf_t*)get_env(argv, READ_XBUF);
xbuf_cat(reply, "START\n"); xbuf_ncat(reply, read_buff->ptr, read_buff->len); xbuf_cat(reply, "END\n");
// this line is important if I don't use read_buff everything seems OK
// but I need parse read_buff :(
printf("%s\n", read_buff->ptr); // this line is most important
return 200;
}
at first everything seems OK
shell:~$ for I in seq 0 1; do curl -A "" -H "TST: ${I}" 'http://test.com:8080/?read_buf.c&scp=3'; done
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 1
END
execute my loop again
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 1
END
execute my loop again
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 1
END
but there is my problem. Where TST is still 0 ?
execute my loop again
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
execute my loop again
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
START
GET /?read_buf.cscp=3HTTP/1.1
Host: test.com8080
Accept: */*
TST: 0
END
Why? Due to caching ? How to disable it?
PS: The servlet was executing on G-WAN 4.3.14
Since this question was posted after we replied to it by email, there is little doubt about its true objectives which are very far away from the alleged technical pretext.
To let people be judge, here is the reply we emailed that guy:
All users will see what your script is displaying.
And your script is not using any user-session. Under concurrency, this script displays the same information in the same manner for all clients.
G-WAN detects that, and this is triggering its cache because your script is slow (probably due to the print-to-console).
No such application would exist in the real world: you would use personalized URI parameters, or POST entities or even Cookies - something that your test carefully avoids - hence its irrelevance.
Besides, you might benefit from reading the G-WAN FAQs:
http://gwan.ch/faq#cache
Finally, G-WAN was not created to compete with any existing Web framework. The goal was merely to satisfy the needs of our own projects:
http://twd-industries.com/
And here G-WAN fits this task, very well, because we wrote it with the intent to use it properly.

Resources