Skip to content

Commit

Permalink
add a web page fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
owenramber1989 committed Apr 17, 2023
1 parent 9f8cc5b commit 3894299
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions apps/webget.cc
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
#include "address.hh"
#include "socket.hh"

#include <cstdlib>
#include <iostream>
#include <span>
#include <string>
#include <sys/socket.h>

using namespace std;

void get_URL( const string& host, const string& path )
{
cerr << "Function called: get_URL(" << host << ", " << path << ")\n";
cerr << "Warning: get_URL() has not been implemented yet.\n";
void get_URL(const string &host, const string &path) {
TCPSocket tcpsocket{};
std::string buf;
const Address addr = Address(host, "http");
tcpsocket.connect(addr);
const std::string request = "GET " + path + " HTTP/1.1\r\n" +
"Host: " + host +
"\r\nConnection: close\r\n\r\n";
tcpsocket.write(request);
tcpsocket.shutdown(SHUT_WR);
while (!tcpsocket.eof()) {
tcpsocket.read(buf);
std::cout << buf;
buf.clear();
}
tcpsocket.close();
/*
cerr << "Function called: get_URL(" << host << ", " << path << ")\n";
cerr << "Warning: get_URL() has not been implemented yet.\n";
*/
}

int main( int argc, char* argv[] )
{
try {
if ( argc <= 0 ) {
abort(); // For sticklers: don't try to access argv[0] if argc <= 0.
}
int main(int argc, char *argv[]) {
try {
if (argc <= 0) {
abort(); // For sticklers: don't try to access argv[0] if argc <= 0.
}

auto args = span( argv, argc );
auto args = span(argv, argc);

// The program takes two command-line arguments: the hostname and "path" part of the URL.
// Print the usage message unless there are these two arguments (plus the program name
// itself, so arg count = 3 in total).
if ( argc != 3 ) {
cerr << "Usage: " << args.front() << " HOST PATH\n";
cerr << "\tExample: " << args.front() << " stanford.edu /class/cs144\n";
return EXIT_FAILURE;
}
// The program takes two command-line arguments: the hostname and "path"
// part of the URL. Print the usage message unless there are these two
// arguments (plus the program name itself, so arg count = 3 in total).
if (argc != 3) {
cerr << "Usage: " << args.front() << " HOST PATH\n";
cerr << "\tExample: " << args.front()
<< " stanford.edu /class/cs144\n";
return EXIT_FAILURE;
}

// Get the command-line arguments.
const string host { args[1] };
const string path { args[2] };
// Get the command-line arguments.
const string host{args[1]};
const string path{args[2]};

// Call the student-written function.
get_URL( host, path );
} catch ( const exception& e ) {
cerr << e.what() << "\n";
return EXIT_FAILURE;
}
// Call the student-written function.
get_URL(host, path);
} catch (const exception &e) {
cerr << e.what() << "\n";
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
return EXIT_SUCCESS;
}

0 comments on commit 3894299

Please sign in to comment.