Skip to content

Commit

Permalink
lab0: implement get_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengjie-Huang committed May 30, 2022
1 parent 898f0c0 commit 3e44d2c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions apps/webget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ using namespace std;

void get_URL(const string &host, const string &path) {
// Your code here.
const uint16_t portnum = ((std::random_device()()) % 50000) + 1025;

// You will need to connect to the "http" service on
// the computer whose name is in the "host" string,
// then request the URL path given in the "path" string.
TCPSocket socket;
socket.connect(Address(host, "http"));
socket.write("Get " + path + "HTTP/1.1\r\n");
auto recvd = socket.read();

// Then you'll need to print out everything the server sends back,
// (not just one call to read() -- everything) until you reach
// the "eof" (end of file).
while (!recvd.empty()) {
cout << recvd;
recvd = socket.read();
}

socket.write("Connection: close\r\n");
socket.close();
cerr << "Function called: get_URL(" << host << ", " << path << ").\n";
cerr << "Warning: get_URL() has not been implemented yet.\n";
}
Expand Down

0 comments on commit 3e44d2c

Please sign in to comment.