I'm new to asio and was follwing a tutorial. After sending a pretty basic http get request to example.com there is no response.
#include<json/json.h>
#include<asio/ts/buffer.hpp>
#include<asio/ts/internet.hpp>
#include<iostream>
#define ASIO_STANDALONE
int main(){
asio::error_code ec;
asio::io_context context;
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("93.184.216.34", ec), 80);
asio::ip::tcp::socket socket(context);
socket.connect(endpoint, ec);
std::cout << ec.message() << '\n';
if(socket.is_open())
{
std::string sRequest = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n";
std::cout << sRequest.data();
socket.write_some(asio::buffer(sRequest.data(), sRequest.size()), ec);
int bytes = socket.available();
std::cout << bytes << '\n';
if(bytes > 0)
{
std::vector<char> vBuffer(bytes);
socket.read_some(asio::buffer(vBuffer.data(), vBuffer.size()), ec);
for(auto c : vBuffer)
{
std::cout << c;
}
}
}
return 0;
}
What is weird is that from tcpdump it seems that there was a response sent back.
16:53:57.116316 IP arch.44680 > 93.184.216.34.http: Flags [S], seq 999815019, win 32120, options [mss 1460,sackOK,TS val 2462554605 ecr 0,nop,wscale 7], length 0
16:53:57.173058 IP arch.35949 > _gateway.domain: 48878+ PTR? 34.216.184.93.in-addr.arpa. (44)
16:53:57.193671 IP _gateway.domain > arch.35949: 48878 NXDomain 0/1/0 (115)
16:53:57.236277 IP 93.184.216.34.http > arch.44680: Flags [S.], seq 729800927, ack 999815020, win 65535, options [mss 1452,sackOK,TS val 3857096832 ecr 2462554605,nop,wscale 9], length 0
16:53:57.236306 IP arch.44680 > 93.184.216.34.http: Flags [.], ack 1, win 251, options [nop,nop,TS val 2462554725 ecr 3857096832], length 0
16:53:57.236454 IP arch.44680 > 93.184.216.34.http: Flags [P.], seq 1:52, ack 1, win 251, options [nop,nop,TS val 2462554726 ecr 3857096832], length 51: HTTP: GET /index.html HTTP/1.1
16:53:57.236490 IP arch.44680 > 93.184.216.34.http: Flags [F.], seq 52, ack 1, win 251, options [nop,nop,TS val 2462554726 ecr 3857096832], length 0
16:53:57.357833 IP 93.184.216.34.http > arch.44680: Flags [.], ack 52, win 128, options [nop,nop,TS val 3857096953 ecr 2462554726], length 0
16:53:57.358428 IP 93.184.216.34.http > arch.44680: Flags [P.], seq 1:1613, ack 53, win 128, options [nop,nop,TS val 3857096953 ecr 2462554726], length 1612: HTTP: HTTP/1.1 200 OK
16:53:57.358429 IP 93.184.216.34.http > arch.44680: Flags [F.], seq 1613, ack 53, win 128, options [nop,nop,TS val 3857096953 ecr 2462554726], length 0
16:53:57.358458 IP arch.44680 > 93.184.216.34.http: Flags [R], seq 999815072, win 0, length 0
16:53:57.358487 IP arch.44680 > 93.184.216.34.http: Flags [R], seq 999815072, win 0, length 0
Your vector is empty because
bytesis a TOCTOU race. You printedbytes, and it is likely 0. At least, for me it is:Instead, accept a dynamic read until
"\r\n\r\n":Actually, you need to do the same on the write, because
write_somemay write only part of the message. Also, don't manually specify the buffer and size, reducing room for errors.E.g. Live On Coliru
Showing
Out of the box
Of course, now you woud need to parse the headers, get the content-length, read the body (taking into account the part of the body that was already received in
vBuffer).That's only scratching the surface because the response may use chunked encoding.
You have to implement all that.
OR you could use a library like Beast to do it for you instead of reinventing the wheel (likely with security vulnerabilities).
Live On Coliru
Printing e.g.