A tiny HTTP client
| connect | |
| send the GET | |
| Read the response |
connect
Syscall 42 (connect) takes the socket, a sockaddr_in with the server address and port, and its length. The port goes in network byte order: swap the bytes with a shift, or let the assembler do it:
%define SYS_CONNECT 42
mov rax, SYS_CONNECT
mov rdi, fd
mov rsi, server_addr
mov rdx, 16
syscall
send the GET
Write the request bytes with sys_write on the connected socket. A minimal HTTP/1.0 GET ends with two CRLF pairs, and the server closes the connection when the response is done:
section .data
request db "GET / HTTP/1.0", 13, 10, 13, 10
reqlen equ $ - request
Read the response
Read with sys_read into a buffer in a loop until it returns zero. The response is headers, a blank line, then the body - split on the first double CRLF if you only want the body.
Next: Output formats: bin, elf, elf64
Article author: Arthur Isaev