how do I write a response directly in http-kit? I'm building a tunneling proxy and need the very first response to be exactly "HTTP/1.1 200 Connection established\r\n\r\n", but it seems http-kit renders that as a body, even if I do something like {:status "HTTP/1.1 200 Connection established"}
Did you configure http-kit to use a proxy?
Here's an example from their test-suite:
#(http/get "https://127.0.0.1:9898/get"
{:proxy-url "http://127.0.0.1:4348"})
Related
I have succesfully retrieved the data using a java HttpURLConnection and setting request method as "GET". I would like to implement this in c++ as well (I'm using poco). The URL I am trying to access is "https://open.faceit.com/data/v4".
When I send the request I get a status code 400 (Bad Request) and the response message "400 The plain HTTP request was sent to HTTPS port". Why would using HTTP in java work, but using POCO in c++ I get this error? Should I be using HTTPSClientSession instead, and if so why was I able to use HTTP in java?
java code that gives return code 200
URL url = new URL(dataURL);
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setRequestMethod("GET");
hc.setRequestProperty("Authorization", "Bearer " + code);
hc.setDoOutput(true);
int rCode = hc.getResponseCode();
c++ thats giving me return 400
Poco::URI uri("https://open.faceit.com/data/v4/players?nickname=FadesfasT&game=CSGO");
std::string path(uri.getPathAndQuery());
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
request.add("Authorization", "Bearer" + token);
request.setContentType("application/json");
session.sendRequest(request);
HTTPResponse response;
std::istream& in_stream = session.receiveResponse(response);
std::ostringstream out_stream;
Poco::StreamCopier::copyStream(in_stream, out_stream);
std::cout << out_stream.str() << std::endl;
The problem with your C++ snippet is that you are mixing an URL containing a HTTPS address with a request targeting HTTP.
My uneducated guess about why the issue does not appear in Java is that it recognizes the 'https' in the address and automatically uses an appropriate handler for that. Fact is, in C++ or at least in POCO you have to pick the appropriate request yourself.
Basically you have 3 options:
Use a HTTP URL with the HTTP request that you have already written.This does not really seem to be an option, because your URL redirects HTTP to HTTPS so this would not work.
Use a HTTPS URL and change your request to HTTPS. It might be more difficult than option 1, but not that hard and there exists a SO question discussing this. To summarize:
Should I be using HTTPSClientSession instead [...] ?
Yes, if you decide to go with this option
Use a library other than POCO, which seems to be the solution in your case as mentioned in the comments. I for example know of CURL and libhttp, which imo are at least as easy to use as POCO, if not easier. The links lead to pages containing examples on how to execute HTTPS requests.
I want to create a Http Server to send an MJPEG Stream. In the first place i therefore want to create a Simple Version that just sends some html/text. I've already Managed to set up a TCP-Server but I don't have any clue about how to "act" like an http Server.
What I did:
Created an TCP-Server. When a client Connects a TCP-Socket is created. Then I implemented a ReadyRead SLOT which gots executed when the Browser sends the "GET" Request to the Server.
GET / HTTP/1.1 Host: 127.0.0.1:8889 User-Agent:
Mozilla/5.0...
Then I run following Code
QByteArray header = "HTTP/ 1.1 200 OK\r\n";
m_Client->write(header);
QByteArray ContentType = "Content-Type: text/html\r\n";
m_Client->write(ContentType);
QByteArray Body = "Test";
m_Client->write(Body);
m_Client->close();
But what I see in the Browser is
HTTP/ 1.1 200 OK Content-Type: text/html Test
So what am I doing wrong? I thought about receiving the Client GET Request, sending the Header, Mimes and the Content in Return and Then Closing the Connection.... Is this Method wrong or Just the way I coded it?
You have an extra space between the / and 1.1, and you are missing an empty line between the headers block and the response body.
I have a mongoose server, with commands callable with AJAX. I get a CORS error if I call it without sending HTTP headers from mongoose (but visiting the address with the browser works just fine), but when I do send headers, it may take up to a minute before I get a response (but it does work), both with AJAX and the browser. My reply code:
//without headers
mg_printf(conn,reply.c_str());
//with headers
mg_printf(conn,"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\n"
"Cache-Control: no-cache\n"
"Access-Control-Allow-Origin: *\n\n"
"%s\n", reply.c_str());
How can I speed this up? Am I sending my headers wrong?
Ok, I found a solution, it works if I first check whether the request is an api call or not, and only send the headers when it is.
The reason mongoose is slow is because it waits for the rest of the content until it times out. And the reason it waits is because you do not set Content-Length, in which case the "end of a content" marker is when connection closes.
So the correct solution is:
Add Content-Length header with correct body length, OR
Alternatively, use mg_send_header() and mg_printf_data() functions, in which case you don't need to bother with Content-Length cause these functions use chunked encoding.
When I send a http request using a wrong server address like 127.0.0.1 as the server address of a URL, the libcurl returns CURLE_OK and get me the http code 0. However, I get http code 404 when I send the same request with IE. Does anyone know how can I get an error code rather than 0 with libcurl when sending request like that.
libcurl returns CURLE_OK when the transfer went fine. Getting a 404 from a HTTP server is considered a fine transfer. You can make >=4xx HTTP response codes cause a libcurl error by setting the CURLOPT_FAILONERROR option.
Alternatively, and this may be the nicer way, you extract the HTTP response code after the transfer, with for example curl_easy_getinfo() to figure out the HTTP response code to see what the HTTP server thought about the resource you requested.
Try using it to visit a site that's actually running a web server, and try to retrieve a file that doesn't exist. For example, http://www.google.com/404. Your browser is almost certainly not actually getting a 404 from visiting 127.0.0.1, even if it's telling you that's what it got.
Getting Response is null error while receiving HTTP response.
I am developing an sample small HTTP server in C using row sockets.
There are actually 2 servers in my application one is standard Apache server which I am using for serving HTML pages and my small server will respond to only XMLHttpRequest sent from the Javascript within the HTML pages.
I am sending request from JavaScript as follows:
var sendReq = new XMLHttpRequest();
endReq.open("POST", "http://localhost:10000/", true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleResult;
var param = "REQUEST_TYPE=2002&userName=" + userName.value;
param += "&password=" + password.value;
sendReq.send(param);
When I send this request I receive following Request in my server code:
OPTIONS / HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:7777
Access-Control-Request-Method: POST
I have replied to this Request as follows using socket write function:
HTTP/1.1 200 OK\n
Access-Control-Allow-Origin: *\n
Server: PSL/1.0 (Unix) (Ubuntu/Linux)\n
Access-Control-Allow-Methods: POST, GET, OPTIONS\n
Accept-Ranges: bytes\n
Content-Length: 438\nConnection: close\n
Content-Type: text/html; charset=UTF-8\n\n
I don`t know What should be the HTTP actual response to be sent on request of OPTIONS.
After this I get my Actual POST request that I have sent from JavaScript and then I respond back with
HTTP/1.1 200 OK\n\n
And then at the browser end get error Response is null.
So how to send headers/data as HTTP Response using row sockets in 'C' and how to respond to OPTIONS request. Can someone explain me by giving some example?
It's hard to understand your question, but I believe you are pointing to this as the response giving you trouble:
HTTP/1.1 200 OK\n\n
You should be including other fields, especially the Content-Length and Content-Type. If you're going to build your own HTTP server, then you should review the protocol specifications.
That said, it's not at all clear why you need to replace the HTTP server instead of using either CGI or another server side language (PHP, Java, etc). This is significantly reducing your portability and maintainability.
Finally, you appear to be transmitting the password in the request. Make sure that this is only done over some kind of encrypted (HTTPS) or else physically secured connection.
I'm not sure what you're asking, but you might find the following useful:
HTTP Made Really Easy
HTTP/1.1 rfc2616.txt
MAMA - Opera Developer Community
I found them all quite useful when I was writing a HTTP client.
This problem had occured as after processing the OPTIONS request by our server, any subsequent requests made, for some reason, were required to be responded back with "Access-Control-Allow-Origin: *" along with other normal headers and response body.
After providing this line in our responses, I always got the desired responseText/responseXML in my javascript.