char* escape "\" are being lost in libcurl's POSTFIELD - c++

I have two examples of using POSTFIELDS - the first works fine and the second does not
I am not sure if this is down to libcurl or the compiler removing the escape "\" from the character 'send'.
if I make 'send' an std::string rather that a char* then I get a bad request error saying "unexpected token in Json"
this is a comparison of how I am using POSTFIELDS
char* send;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"var\":\"info\"}");
and
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, send);
I get a 500 error that shows as
Unhandled error in PUT /random/page 500 AssertionError [ERR_ASSERTION]: The data argument must be an object
There is also problem with the output from libcurl
> PUT /random/page HTTP/1.1
Host: 127.0.0.1:3000
Accept: application/json
Content-Type: application/json
charsets: utf-8
Content-Length: 364
* upload completely sent off: 364 out of 364 bytes
< HTTP/1.1 500 Internal Server Error
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Date: Wed, 14 Aug 2019 11:30:55 GMT
< Connection: keep-alive
< Content-Length: 62
The content length at the bottom does not match the request

Related

Getting java.net.URISyntaxException: Illegal character in path at index 7: http:/${Bearer}

I have an API name loginUser, which generates the authorization Token, that is to be passed in other subsequent APIs.
Below is the response:-
HTTP/1.1 200 OK
Date: Sat, 10 Nov 2018 07:08:45 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 71
Connection: keep-alive
Server: nginx/1.10.3 (Ubuntu)
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: date, authorization, x-powered-by, connection, server, access-control-allow-origin, content-type, content-length, x-final-url
authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIyYmEyYjc1My03NWEwLTQxNGYtYWFiOC0zZGY1M2I4YmIwMDEiLCJpc0Nvb2siOnRydWUsImlhdCI6MTU0MTgzMzcyNX0.3FRVpHm4EF2Ahzzy-OjbZ2EeZto6-hSFKHNtG5wcjBs
Where I want to fetch the authorization.
I'm using Regular Expression Extractor, but it is throwing below error (URISyntaxException: illegal character in the path)
Snapshots:-
Step_1
Step_4
I have seen couple of queries related to this Error but not in Jmeter,
And i tried using % also, to get rid out of this error, but that didn't work out.
Thanks in advance.
Authorization is sent/receive in headers,
Change Step 2, Field to check should be Response Headers to get the value

Deflating POCO HttpResponse with gzip cuts the content

I am using POCO 1.7.8 to write a HTTP server. Problem is when using gzip for deflating the response data:
std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "gzip");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_GZIP);
deflater << content;
deflater.close();
Response for the client is:
HELLO WORLD, THIS IS LONGISH STRING
Response headers:
Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: gzip
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 07:52:17 GMT
If I change this to use ZLIB and set the Content-Encoding to deflate, the whole response is correctly returned from the server:
std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "deflate");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_ZLIB);
deflater << content;
deflater.close();
Response for the client is:
HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT
Response headers:
Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: deflate
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 08:07:36 GMT
I tried to find examples how this should be done in the POCO server but couldn't find any and I am a bit stuck now with this. Any help is appreciated!
Are you sure you're setting the Content-Length header correctly for your compressed response? Alternatively try enabling chunked transfer encoding before calling send().
response->setChunkedTransferEncoding(true);

how to capture the Cookies in HttpQueryInfo using wininet c++

Currently I'm facing an issue in capturing the Cookies which is coming as part of response. I'm using WinInet for my connection.
After sending the request using HttpSendRequest, I'm using HttpQueryInfo to query the response in the below order
HTTP_QUERY_STATUS_CODE
HTTP_QUERY_SET_COOKIE
HTTP_QUERY_CONTENT_TYPE
HTTP_QUERY_CONTENT_LENGTH
Finally I'm reading body content using InternetReadFile.
Server has the loadbalance installed. all the response sent from LB has "Set-Cookies" enabled.
If I get any response without "Set-Cookies" in header then my code is reading the content successfully.
When there is "Set-Cookies" field in header then the issue comes. It is resulting out in "Header Not Found" when I query for "HTTP_QUERY_CONTENT_TYPE"
Below is the sample response header which is success
HTTP/1.1 200 OK
Cache-Control: private
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 17 Dec 2015 10:05:00 GMT
Content-Length: 107
Content-Type: text/html; Charset=utf-8
Via: 1.1 TestServer:80 (Cisco-WSA/8.8.0-085)
Connection: keep-alive
Below is the sample response header which is error out.
HTTP/1.1 200 OK
Cache-Control: private
Server: Microsoft-IIS/8.5
Set-Cookie: MYID=B3C5D2B2%2D0426%2D473F%2DACF6%2DF19BAC3F5A6C%3A%3A88757401; path=/
X-Powered-By: ASP.NET
Date: Thu, 17 Dec 2015 10:07:20 GMT
Content-Length: 277
Content-Type: text/html; Charset=utf-8
Via: 1.1 TestServer:80 (Cisco-WSA/8.8.0-085)
Connection: keep-alive
Work around found,
While querying the response header, if I add HTTP_QUERY_COOKIE in the sequence then the issue is getting resolved but I'm not sure on this is correct or not.
Can any one help me?
Why SET_COOKIE is not able to catch the "set-cookies"
What is the difference between HTTP_QUERY_SET_COOKIE and HTTP_QUERY_COOKIE?
What will be the impact if I use HTTP_QUERY_COOKIE to overcome this issue?
Thanks
Vijay

determining HTTP version using raw sockets in C

I am trying to create raw sockets in C/C++, then create a request message and then send that message to a target server. If the port I specify happens to be 80 I want to send a HTTP request to determine the HTTP version that the target server is using. For e.g I send GET HTTP/1.0 to www.google.com.
For some servers it returns HTTP/1.1 400 bad request. While in some cases it responds with an XML message. I know the GET command is wrong since I am not specifying any object to actually GET. So is there a generic way to do this?
Try:
HEAD / HTTP/1.0\r\n
\r\n
Or:
GET / HTTP/1.0\r\n
\r\n
The first line of the servers response should contain the HTTP version. Note that some servers will return 400 Bad Request if the Host: <hostname> is omitted from the header (which is not required in 1.0, but in 1.1). I would do:
Try:
HEAD / HTTP/1.0\r\n
Host: <hostname>\r\n
\r\n
Or:
GET / HTTP/1.0\r\n
Host: <hostname>\r\n
\r\n
If you don't require the message body, you should use HEAD as it will require less data to receive.
You will have to progressively try each version of HTTP. For example, if I query google.com with HTTP 1.0, it will respond with HTTP 1.0:
$ printf "HEAD / HTTP/1.0\nHost: google.com\n\n" | nc google.com 80
HTTP/1.0 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Tue, 26 Nov 2013 19:44:42 GMT
Expires: Thu, 26 Dec 2013 19:44:42 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
If I query it with HTTP 1.1, it will respond with HTTP 1.1:
$ printf "HEAD / HTTP/1.1\nHost: google.com\n\n" | nc google.com 80
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Tue, 26 Nov 2013 19:44:47 GMT
Expires: Thu, 26 Dec 2013 19:44:47 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
GET / HTTP/1.0\r\n
Host: www.google.com\r\n
\r\n
This is a basic HTTP request. Alternatively, you can use HEAD instead of GET, sinse you only want the headers, but some basic servers don't recognize HEAD as a valid method.
Not every server will support HTTP/1.0, they will most likely ignore this and answer as HTTP/1.1, others will just ape the version in your request and not really mean it.
It can be frustrating to try to determine the HTTP version in a random server. Perhaps you should instead use 1.1 in the request and see if the server answers with 1.0, I believe it may be the safest way to know if the server is at least giving a damn.

Error 411 (Length Required) in post request with header, but header has Content-Length. libCurl

I use this options:
curl_easy_setopt(curl, CURLOPT_URL, urlUpload);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
It must be post request with header. And in header variable there is length
***
char sizeStr[50];
sprintf(sizeStr, "Content-Length: %d", body.length());
***
header = curl_slist_append(header, sizeStr);
***
What I'm trying to do is to upload video to YouTube, I'm using their manual
And I receive such error.
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<title>Error 411 (Length Required)!!1</title>
<style>
*many symbols here*
</style>
<a href=//www.google.com/ id=g><img src=//www.google.com/images/logo_sm.gif alt=Google></a>
<p><b>411.</b> <ins>Thatв€™s an error.</ins>
<p>POST requests require a <code>Content-length</code> header. <ins>Thatв€™s all we know.</ins>
Maybe I must use some other CURLoptions?
UPDATE:
when I set
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
I also receive
* About to connect() to uploads.gdata.youtube.com port 80 (#0)
* Trying 74.125.XX.XXX... * connected
* Connected to uploads.gdata.youtube.com (74.125.XX.XXX) port 80 (#0)
> POST /feeds/api/users/default/uploads HTTP/1.1
Accept: */*
Host: uploads.gdata.youtube.com
Authorization: GoogleLogin auth=DQAAAIkAAACTK9tZPCTY1XQvXGkg4qkaIuZ1QO-Bh6- ZyzOHuigFNC_gR4Piep4NljAjdOP4s-k7vqj-j4LdckXM9jxzlElgtaxr- CShI1vIWkjm5ZtFsj3a9v1YqFmjIkEi3NCP2ON18D9jmXSIarPqprWiOK0n3oxznCBhm4osXwJ1yRstVVM5bG5mOlC331eMCrOKp3E
GData-Version: 2
X-GData-Key: key=AI39si59VMkm6DATDmfG_Df6D23jfto3xRVfbAEMrFBv035pdRZ5AYMPsRXbGLCRXXnK5jz6KCSWSkuXOTrlDIIKWy7Le9fkQQ
Slug: screen.avi
Content-Type: multipart/related; boundary="d31fcjR2"
Content-length: 910273
Connection: close
* HTTP 1.0, assume close after body
< HTTP/1.0 411 Length Required
< Content-Type: text/html; charset=UTF-8
< Content-Length: 11791
< Date: Fri, 02 Sep 2011 16:09:58 GMT
< Server: GFE/2.0
<
* Closing connection #0
This error was because in the authentication string that I receive from YouTube was in the end the new line symbol, I erase it and this error disappeared.
CURLOPT_POSTFIELDS will make a content-length get added automatically by libcurl, no need to make a custom one. However, as you're already sending a content-length header that is clearly not the missing length the server is talking about.
Your request also sends a "Connection: close" so there's something more of the code that you didn't show us.