L
L
LittleBuster2016-08-23 17:31:04
C++ / C#
LittleBuster, 2016-08-23 17:31:04

Why doesn't the browser understand responses from a self-made Web Server?

Firefox does not want to open the page in any way, I don’t understand why, I haven’t tried anything yet. struct tcp_server, struct tcp_client - high-level wrappers over regular Linux sockets.

#include "tcpserver.h" 
#include <stdio.h>
#include <stdlib.h>

#define DATA_SIZE 1024


static void new_session(struct tcp_client *client, void *data)
{
  char answ[257];
  char page[DATA_SIZE];

  memset(answ, 0x00, 257);
  strcpy(answ, "HTTP/1.1 200 OK\r\n"
         "Version: HTTP/1.1\r\n"
         "Content-Type: text/html; charset=utf-8\r\n"
         "Content-Length: 4"
         "\r\n\r\n"
         "serg");	

  tcp_client_recv(client, page, DATA_SIZE);	
  if (!tcp_client_send(client, answ, strlen(answ)))
    puts("fail sending answare.");
  else
    puts("Sended.");
  puts(page);
}

static void accept_error(void *data)
{
  puts("Accepting client fail!");
}

int main(int argc, char const *argv[])
{
  struct tcp_server server;

  tcp_server_init(&server);
  tcp_server_set_newsession_cb(&server, new_session, NULL);
  tcp_server_set_accepterr_cb(&server, accept_error, NULL);
  if (!tcp_server_bind(&server, 8080, 100)) {
    puts("Fail starting web server");
    return -1;
  }
  return 0;
}

666a397b69a441509a4e5e3e67cca13f.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LittleBuster, 2016-08-23
@LittleBuster

There was an incorrect implementation of the high-level add-on over the socket. He waited for a certain number of bytes to be received, and if less came, he waited for the rest to come. Because of this, tcp_client_recv hung and the response to the browser did not go away.

V
Vladimir Dubrovin, 2016-08-23
@z3apa3a

Do you put a linger (SO_LINGER) on the socket? If a linger is not installed and you terminate the application immediately after sending data to the socket, then the data may not have time to leave.
PS
Judging by the Proxy-Authorization header, you also use some kind of proxy, and this proxy behaves strangely. this heading should not go outside. If it's not a secret, what software behaves like this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question