views:

141

answers:

4

I thought I had this all figured out, but now that I'm writing a webserver, something is not quite working right.

The app listens on a port for incoming requests, and when it receives one, it reads everything up to the sequence "\r\n\r\n". (Because that signifies the end of the headers - yes, I am ignoring possible POST data.)

Now, after it reads that far, it writes to the socket the response:

HTTP/1.1 200 OK\r\n
Host: 127.0.0.1\r\n
Content-type: text/html\r\n
Content-length: 6\r\n
\r\n
Hello!

However, when Firefox or Chrome tries to view the page, it won't display. Chrome informs me:

Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error.

What am I doing wrong?


Here is some of the code:

QTcpSocket * pSocket = m_server->nextPendingConnection();

// Loop thru the request until \r\n\r\n is found
while(pSocket->waitForReadyRead())
{
    QByteArray data = pSocket->readAll();

    if(data.contains("\r\n\r\n"))
        break;
}

pSocket->write("HTTP/1.0 200 OK\r\n");

QString error_str = "Hello world!";

pSocket->write("Host: localhost:8081\r\n");
pSocket->write("Content-Type: text/html\r\n");
pSocket->write(tr("Content-Length: %1\r\n").arg(error_str.length()).toUtf8());
pSocket->write("\r\n");
pSocket->write(error_str.toUtf8());

delete pSocket;
A: 

Try it with telnet or nc (netcat) to debug it. Also, is it possible you're sending double newlines? I'm not sure your language, so if your print appends a newline, switch to:

HTTP/1.1 200 OK\r

It's also handy to have a small logging/forwarding program for debugging socket protocols. I don't know any offhand, but I've had one I've used for many years and it's a lifesaver when I need it.

[Edit] Try using wget to fetch the file; use the -S -O file to save the output to a file and see what's wrong that way.

[#2]

pSocket->write(error_str.toUtf8());

delete pSocket;

I haven't used Qt for a long time, but do you need to flush or at least close the socket before deleting?

Carter Galle
I'm using C++. (The Qt Framework.)
George Edison
on what platform?
Carter Galle
@Carter: Linux 64-bit.
George Edison
Great, install **wget** if it isn't already. You'll be glad after you start using it (if you don't already know it).
Carter Galle
@Carter: I have `wget` installed - I'll try that.
George Edison
**Aha!** "HTTP request sent, awaiting response... No data received.Retrying."
George Edison
A: 

What you’ve shown looks okay, so it must be that you’re actually sending something different. (I presume that you're entering "http://127.0.0.1" in your browser.)

Download the crippleware version of this product and see what it reports:

http://www.charlesproxy.com/

danorton
I tried WireShark but it wasn't showing the response my app was supposed to be making... so you might be onto something here.
George Edison
+4  A: 

Could the problem be that you're not flushing and closing the socket before deleting it?

EDIT: George Edison answered his own question, but was kind enough to accept my answer. Here is the code that worked for him:

pSocket->waitForBytesWritten();
TokenMacGuy
No problem! And thanks... you got it before I did.
George Edison
+5  A: 

I figured it out!

After writing the data to the socket, I have to call:

pSocket->waitForBytesWritten();

...or the buffer does not get outputted.

George Edison