Skip to content

Commit

Permalink
Implemented a small HTTP client called steal, minor terminal tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
fido2020 committed Feb 9, 2021
1 parent a4fec07 commit 1c5409a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
75 changes: 75 additions & 0 deletions Applications/Steal/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <vector>

#include <lemon/core/url.h>

#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char** argv){
if(argc < 2){
std::cout << "Usage: " << argv[0] << "[options] <url>\nSee " << argv[0] << "--help";
return 1;
}

Lemon::URL url(argv[1]);

if(!url.IsValid() || !url.Host().length()){
std::cout << "steal: Invalid/malformed URL";
return 2;
}

uint32_t ip;
if(inet_pton(AF_INET, url.Host().c_str(), &ip) > 0){

} else {
return -1; // No DNS support yet
}

int sock = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in address;
address.sin_addr.s_addr = ip;
address.sin_family = AF_INET;
address.sin_port = htons(80);

if(int e = connect(sock, reinterpret_cast<sockaddr*>(&address), sizeof(sockaddr_in)); e){
perror("steal: Failed to connect");
return 3;
}

std::ostream& out = std::cout;

char request[4096];
int reqLength = snprintf(request, 4096, "GET / HTTP/1.1\r\n\
Host: %s\r\n\
User-Agent: steal/0.1\r\n\
Accept: */*\r\n\
\r\n", url.Host().c_str());

if(ssize_t len = send(sock, request, reqLength, 0); len != reqLength){
if(reqLength < 0){
perror("steal: Failed to send data");
} else {
std::cout << "steal: Failed to send all " << reqLength << " bytes (only sent " << len << ").\n";
}

return 4;
}

char receiveBuffer[4096];
while(ssize_t len = recv(sock, receiveBuffer, 4096, 0)){
if(len < 0){
perror("steal: Failed stealing data");
return 5;
}

out.write(receiveBuffer, len);

if(len < 4096){
break; // No more data to recieve
}
}

return 0;
}
2 changes: 1 addition & 1 deletion Applications/Terminal/colours.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ rgba_colour_t coloursProfile2[] = {
RGBAColour::FromRGB(0x06b6ef), // Blue
RGBAColour::FromRGB(0x815ba4), // Magenta
RGBAColour::FromRGB(0x5bc4bf), // Cyan
RGBAColour::FromRGB(0xa39e9b), // White
RGBAColour::FromRGB(0xe7e9db), // White
{ 0, 0, 0, 255 },
{ 0, 0, 95, 255 },
{ 0, 0, 135, 255 },
Expand Down
4 changes: 3 additions & 1 deletion Applications/Terminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TermState defaultState {
.blink = 0,
.reverse = 0,
.strikethrough = 0,
.fgColour = 7,
.fgColour = 15,
.bgColour = 0,
};

Expand Down Expand Up @@ -459,6 +459,8 @@ void PrintChar(char ch){
curPos.y++;
curPos.x = 0;
Scroll();

paintAll = true;
break;
case '\b':
if(curPos.x > 0) curPos.x--;
Expand Down
5 changes: 5 additions & 0 deletions Applications/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ minesweeper_src = [
'Minesweeper/main.cpp'
]

steal_src = [
'Steal/main.cpp'
]

executable('fileman.lef', fileman_src, cpp_args : application_cpp_args, link_args : ['-llemon', '-lfreetype', '-lz', '-lpng'], install : true)
executable('lsh.lef', lsh_src, cpp_args : application_cpp_args, link_args : ['-llemon'], install : true)
executable('shell.lef', shell_src, cpp_args : application_cpp_args, link_args : ['-llemon', '-lfreetype', '-lz', '-lpng'], install : true)
Expand All @@ -80,3 +84,4 @@ executable('lemonmonitor.lef', lemonmonitor_src, cpp_args : application_cpp_args
executable('ipctest.lef', ipctest_src, cpp_args : application_cpp_args, install : true)
executable('jsondump.lef', jsondump_src, cpp_args : application_cpp_args, install : true, link_args : ['-llemon'])
executable('minesweeper.lef', minesweeper_src, cpp_args : application_cpp_args, link_args : ['-llemon', '-lfreetype', '-lz', '-lpng'], install : true)
executable('steal.lef', steal_src, cpp_args : application_cpp_args, link_args : ['-llemon'], install : true)

0 comments on commit 1c5409a

Please sign in to comment.