forked from Kiprey/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebget.cc
60 lines (48 loc) · 1.79 KB
/
webget.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "tcp_sponge_socket.hh"
#include "util.hh"
#include <cstdlib>
#include <iostream>
using namespace std;
void get_URL(const string &host, const string &path) {
// Your code here.
// 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.
// 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).
Address addr(host, "http");
FullStackSocket http_tcp;
http_tcp.connect(addr);
http_tcp.write("GET " + path + " HTTP/1.1\r\n");
http_tcp.write("HOST: " + host + "\r\n");
http_tcp.write("Connection: close\r\n");
http_tcp.write("\r\n");
while (!http_tcp.eof())
cout << http_tcp.read();
http_tcp.wait_until_closed();
}
int main(int argc, char *argv[]) {
try {
if (argc <= 0) {
abort(); // For sticklers: don't try to access argv[0] if argc <= 0.
}
// 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: " << argv[0] << " HOST PATH\n";
cerr << "\tExample: " << argv[0] << " stanford.edu /class/cs144\n";
return EXIT_FAILURE;
}
// Get the command-line arguments.
const string host = argv[1];
const string path = argv[2];
// Call the student-written function.
get_URL(host, path);
} catch (const exception &e) {
cerr << e.what() << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}