Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Trying to serve html to browser on tacopie server #6

Closed
vivekvrao opened this issue Mar 26, 2017 · 2 comments
Closed

Trying to serve html to browser on tacopie server #6

vivekvrao opened this issue Mar 26, 2017 · 2 comments

Comments

@vivekvrao
Copy link

I am trying to serve up some html content to a browser over a tacopie server. This was my message handler - where I tried both sync and async writes and trying to close the connection when done. Neither works, maybe because I dont understand how the write/disconnect works. Any ideas? thank you!

	void on_new_message(const std::shared_ptr<tacopie::tcp_client>& client, const tacopie::tcp_client::read_result& res) {
		if (res.success) {
			std::string resp = "this is the response";
			std::string fullResp = "HTTP/ 1.1 200 OK\r\nContent-Type: type/html\r\nConnection: Closed\r\n Content-Length: "
				+ std::to_string(resp.length()) + std::string("\r\n\r\n") + resp;

			std::vector<char> buf(fullResp.begin(), fullResp.end());
			client->get_socket().send(buf, fullResp.length());
			//client->async_write({ buf, nullptr });
			client->disconnect();
		}
		else {
			info("WebClient disconnected");
			client->disconnect();
		}
	}
@Cylix
Copy link
Owner

Cylix commented Mar 29, 2017

Hi,

Sorry for the time to reply, I finally could have a look.

I tried your code and it first worked well over curl and netcat, but apparently not with a browser (I tried chrome and safari).
So my guess was that there is an issue with the content of your request.

I double checked with the expected HTTP format, and there is indeed an issue in your response.

  • HTTP/ 1.1 There is an extra space after the /
  • \r\n Content-Length: : There is an extra space Before the Content

When I changed your output into:
HTTP/1.1 200 OK\r\nContent-Type: type/html\r\nConnection: Closed\r\nContent-Length:

it worked perfectly fine :)

I close this issue, but feel free to re-open it if necessary :)

Best

@Cylix Cylix closed this as completed Mar 29, 2017
@Cylix
Copy link
Owner

Cylix commented Mar 29, 2017

BTW, my final code is the following one:

void
on_new_message(const std::shared_ptr<tacopie::tcp_client>& client, const tacopie::tcp_client::read_result& res) {
  if (res.success) {
    std::cout << "write" << std::endl;
    std::string resp     = "this is the response";
    std::string fullResp = "HTTP/1.1 200 OK\r\nContent-Type: type/html\r\nConnection: Closed\r\nContent-Length: "
                           + std::to_string(resp.length()) + std::string("\r\n\r\n") + resp;

    std::vector<char> buf(fullResp.begin(), fullResp.end());
    client->async_write({buf, [=](tacopie::tcp_client::write_result&) {
                           std::cout << "write cb" << std::endl;
                           client->disconnect();
                         }});
  }
  else {
    std::cout << "disconnect" << std::endl;
    client->disconnect();
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants