AJAX response not valid in C++ but Apache - c++

I want to make a server written in C++ to power my game. I learned the basics of sockets and wrote a basic chat program that worked well. Now I want to create an HTTP server like Apache, but only for the AJAX request-response part.
I think just for the beginning i copied one Apache response text, and i sent the exact response with the C++ server program.
The problem that is that the browser (Firefox) connnects to the apache and everything works fine, except all of the requests get a correct response.
But if i send this with the C++ client, then FireBug tells me that the response status is OK (200) but there is no actual response text. (How is this possible?)
This response-text is exactly the same what apache sends. I made a bit-bit comparison and they were the same.
The php file wich is the original response
<?php echo "AS";echo rand(0,9); ?>
And the origional source code:
Socket.h http://pastebin.com/bW9qxtrR
Socket.cpp http://pastebin.com/S3c8RFM7
main.cpp http://pastebin.com/ckExuXsR
index.html http://pastebin.com/mcfEEqPP < this is the requester file.
ajax.js http://pastebin.com/uXJe9hVC
benchmark.js http://pastebin.com/djSYtKg9
jQuery is not needed.
The main.cpp there is lot of trash code like main3 and main4 functions, these do not affect the result.
I know that the response stuff in the C++ code is not really good because the connection closing is not the best; I will fix that later now I want to send a success response first.

the problem:
the index.html is served through apache on port 80. the browser loads it and starting sending requests.
The request file (program) was on another port , on the 8888 port, which already is a different server which dont enables ajax (dont know why) to get the post data. the program can still communicate with remote servers but cant see the response.
after one whole day i tested a lot with the fiddler program , captured the responses, and that method helped me.
I used the fiddler program to capture the the good answer and to capture the bad. They were the same. After this i turned off my socket application, and forced fiddler to auto respond, and the answer from the 'bad' answer still bat. So after that i replaced the bad with the good and nothing happedned. The bad answer with the good text still bad on the :8888 port but the other on the original :80 port was good, but they were absolutly the same and the same program sended it (fiddler) i think there is something missing if the response is not on the same server address (even not the same port).
after this i thought maybe there is a missing header file, or something ike this.
So i configurated apache to listen on the 80 port, loaded in the index.html. after this i shut down the apache server and changed the port to 8888 and i run the ajax requests and i recognized that they are wrong, but they were sent by the apache, and all of the previorus requests (on the same port) were good. so the problem is only with the ajax stuff :D
many thanks to Tony Lee for the Fiddler suggestion.
Actually there is no solution but there is an answer why the problem exists.

I don't know how you're verifying bit-by-bit - if you used fiddler to capture the traffic then this is a mystery.
I'm going to guess the unsent buffered data is lost when you close the socket. See the MSDN article Graceful Shutdown, Linger Options, and Socket Closure. Call shutdown() before you call closesocket() to ensure a clean shutdown.

Not really an answer to your question, but you might find it useful.
Instead of Apache code you can try libevent. It has functions just to make http servers and it probably will be much faster than Apache code.
Check this link. There is some info about building http server with libevent.

Related

How to capture HTTP and URL data, using a C++ application, from a client request

I have a CGI exe application running on IIS. A client (localhost:8080 for testing) can make a request to the server with any application name (e.g. home, store, account, etc...) and any additional queries (e.g. ?test=1&magic=2). Currently, the exe will load and run the code in it but is unable to receive client input via POST or GET.
I have been recommended to try using WT and similar libraries to capture the HTTP and URL information I need, but I'd like to learn to do it without a separate library.
Using C++, how do I capture HTTP request data from the client, URL parameters, cookie data, etc.. via client GET and POST then store that data in variables and constants for later use?
A CGI app runs as a separate process. It receives client data on stdin, and sends data to the client on stdout. When the webserver receives a client connection, it spawns the CGI process hooking up the connected socket to the stdin and stdout handles.
So, for example, if a client requests http://localhost:8080/home?test=1&magic=2, the CGI's stdin should receive data similar to this:
GET /home?test=1&magic=2 HTTP/1.1
Host: localhost:8080
Cookie: ...
And then it can write to stdout, something like this:
HTTP/1.1 200 OK
Content-Type: desired media type
Content-Length: XXXXX
Set-Cookie: ...
data here
Heres a https server u can compile from src https://bitbucket.org/ptroen/crossplatformnetwork/src/master/OSI/Application/Stub/HTTPSServer/httpsserver.cc
I wrote this and performance is pretty good. See http server if u dont care about encryption. You will need to define two flyweight classes in order to initialize with your specific cgi biz logic.
It supports post put get delete. Havent tested with cookies but wont be hard to implement.

Port number getting stripped out of URL

I have an django based website that's presented through gunicorn with nginx as reverse proxy. It's on port 81.The relevant portion of the configuration file looks like this:
upstream elearning_server {
server localhost:8000;
}
server {
listen 81;
......
If you go to:
http://webaddress:81
the first time after you log in, it removes the port number and fails to connect to the website. If you then put the port number back into the url it works fine.
Has anyone got any suggestions of what I can try to fix this problem.
What is happening is that your web server, as part of the login process is 'redirecting' your browser to a different web address. The problem is that the web address your browser is being redirected to doesn't exist, because it is being redirected to the wrong web address. When you modify the web address, you are correcting this error.
Obviously the solution is to correct this problem in the code, so you are sent straight to the correct web address without you needing to correct this.
The solution depends on your code. It is worth looking at it with the following in mind.
Is there something like:
return HttpResponseRedirect('http://website/dir/')
in your code somewhere? This would need to be altered to include the port number, i.e
return HttpResponseRedirect('http://website:81/dir/')
Or, even better,
return HttpResponseRedirect('/dir/')
(This will mean that it won't matter what port the server is on, because the redirect is 'relative'. This would be the usual way to code this sort of thing.)
If a quick perusal of the source doesn't cast any light, I would suggest that the next step would be to look in the access logs (and maybe error logs) from the web server, and look for '3xx' type messages, probably '301' and '302' messages, and this may give you some clue as to what is happening, and where in the code.

Is there a web service that spits out the entire request that was sent?

I apologize if this isn't "programming" worthy. I'm wondering if a service exists that when the HTTP service is pinged, it echos back the exact same request you made as the response.
The reason I want this is I want to UnitTest a class I made to build requests and send them over a socket. I realize I could just do a Mock object of some sort, but I think that involves more complexity than just making sure the request being sent was properly built.
Ideally, the web service would send the content back as proper HTTP 1.1 with the request info I sent in the body of the response.
Thanks!
Kyle
-- edit --
Just a quick reference to the solution. Point your browser to: http://scooterlabs.com/echo.json or http://scooterlabs.com/echo.xml
This guy seemed to have the same problem as you web service echo test
Refers to some links you might be interested in
I guess there are some uses for a simple echo, but in any kind of a realistic interaction it's going to be pretty hard to isolate just the piece you are trying to test.
A more general approach would be to use a local proxy server, stands as the man in the middle
between you and all remote sites, and can log urls, responses, content and so on.
If you're developing the server side as well as the client, you definitely ought to run a
local mirror of the server site.

How do I get the source URL of the data being recv'd through a socket?

I thought about keeping track of redirects because they would have the next URL in the header until I get a HTTP200 ok, then I would have the FQDN of the source of the 200.
But I don't think that is how a browser does it, my proxy can do a hard redirect of the browser request, but the browser displays the correct URL of the redirected data without having received any HTTP301's.
I want to know what the browser does to get the source of its data and then do that in winsock. Anyone?
Whatever the browser does, it win't retrieve the information from the winsock layer: the sockets only travel in IP addresses and ports. I don't really know much about winsock (I'm UNIX guy) but it seems to sufficiently similar to UNIX socket layer to be quite sure that any transport level information is coming from a higher level abstraction. If you look at the [OSI model]{http://en.m.wikipedia.org/wiki/OSI_model} you see that winsocks is addressing level 5 while the thing you are looking for are level 7. The API typically stay within thecorresponding level. This view seems to be backed up by the [winsock FAQ]{http://tangentsoft.net/wskfaq/intermediate.html#appproto}.
Not all HTTP redirects result in a URL change. A 301 redirect certainly does, but other 3xx redirects do not. If a browser receives a redirect other than 301, it may decide whether to continue displaying the original URL or to display the new redirected URL.
There are ways to redirect without a 301. There's also 302. There's the element
<meta http-equiv="refresh" content="0; url=http://foo.com/">
Finally, there's JavaScript:
<html><head><script language="javascript">
window.location.href = "http://www.bar.com";
</script></head><body></body></html>
The Winsock solution to my question is to use the getpeername() function after connect()'ing to a remote connection. That should return IP:Port info from which I can get the hostname at least.

How can I do an HTTP redirect in C++

I'm making an HTTP server in c++, I notice that the way apache works is if you request a directory without adding a forward slash at the end, firefox still somehow knows that it's a directory you are requesting (which seems impossible for firefox to do, which is why I'm assuming apache is doing a redirect).
Is that assumption right? Does apache check to see that you are requesting a directory and then does an http redirect to a request with the forward slash? If that is how apache works, how do I implement that in c++? Thanks to anyone who replies.
Determine if the resource represents a directory, if so reply with a:
HTTP/1.X 301 Moved Permanently
Location: URI-including-trailing-slash
Using 301 allows user agents to cache the redirect.
If you wanted to do this, you would:
call stat on the pathname
determine that it is a directory
send the necesssary HTTP response for a redirect
I'm not at all sure that you need to do this. Install the Firefox 'web developer' add-on to see exactly what goes back and forth.
Seriously, this should not be a problem. Suggestions for how to proceed:
Get the source code for Apache and look at what it does
Build a debug build of Apache and step through the code in a debugger in such a case; examine which pieces of code get run.
Install Wireshark (network analysis tool), Live HTTP Headers (Firefox extension) etc, and look at what's happening on the network
Read the relevant RFCs for HTTP - which presumably you should be keeping under your pillow anyway if you're writing a server.
Once you've done those things, it should be obvious how to do it. If you can't do those things, you should not be trying to develop a web server in C++.
The assumption is correct and make sure your response includes a Location header to the URL that allows directory listing and a legal 301/302 first line. It is not a C++ question, it is more of a HTTP protocol question, since you are trying to write a HTTP server, as one of the other posts suggests, read the RFC.
You should install Fiddler and observe the HTTP headers sent by other web servers.
Your question is impossible to answer precisely without more details, but you want to send an HTTP 3xx status code with a Location header.