-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpc.java
212 lines (177 loc) · 7.13 KB
/
httpc.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import Network.Builder.GETRequestBuilder;
import Network.Builder.POSTRequestBuilder;
import Network.Builder.RequestBuilder;
import Network.Client.Client;
public class httpc {
static Argument parameter = new Argument();
public static void main(String[] args) {
// args = new String[] { "POST" , "-h", "Content-Length:12", "-d", "Hello world!" , "http://localhost:8080/bob.txt"};
// args = new String[] { "GET" , "http://httpbin.org/get?course=networking&assignment=1"};
for (int i = 0; i < args.length; i++) {
if (args[0].equals("help")) {
try {
args[1] = args[1];
} catch (Exception e) {
System.out.println(help(""));
System.exit(0);
}
System.out.println(help(args[1]));
System.exit(0);
}
}
initArgument(args);
RequestBuilder req = null;
if(parameter.getRequestType().equals("GET "))
{
req = new GETRequestBuilder(parameter.getURL(), parameter.getHeader(),parameter.getBody());
}
else if (parameter.getRequestType().equals("POST "))
{
req = new POSTRequestBuilder(parameter.getURL(), parameter.getHeader(),parameter.getBody());
}
else
{
System.out.println("The request Type is not accepted");
System.out.println(help(""));
System.exit(0);
}
Client net = new Client(req, parameter.getURL());
net.request();
if (parameter.isOutputToFile()) {
outputToFile(net,args[args.length-1],parameter.isVerbose());
System.exit(0);
}
if (parameter.isVerbose()) {
System.out.println(net.getRes().verboseToString(false));
}
else {
System.out.println(net.getRes().toString());
}
}
public static void initArgument(String[] args) {
if (contains(args, "-v")) {
parameter.setVerbose(true);
}
if (contains(args, "-h")) {
setHeaders(args);
}
if (contains(args, "-d")) {
parameter.setData(true);
setBodyD(args);
}
if (contains(args, "-f")) {
parameter.setFile(true);
setBodyF(args);
}
if (args[args.length - 2].equals("-o")) {
parameter.setOutputToFile(true);
}
setURL(args);
if (parameter.isData() && parameter.isFile()) {
System.out.println("Request is not appropriate contains both a file and inline data");
System.exit(0);
}
if (parameter.getRequestType().equals("GET ") && (parameter.isData() || parameter.isFile())) {
System.out.println(" GET Request cannot be used with a file or inline data");
System.exit(0);
}
}
public static String help(String temp) {
switch (temp) {
case "":
return "\n\nusage: httpc get [-v] [-h key:value] URL"
+ "httpc is a curl-like application but supports HTTP protocol only. \nUsage: \nhttpc command [arguments] \nThe commands are:\n"
+ "get executes a HTTP GET request and prints the response. \n"
+ "post executes a HTTP POST request and prints the response. \n" + "help prints this screen. \n"
+ "Use \"httpc help [command]\" for more information about a command.";
case "get":
return "Get executes a HTTP GET request for a given URL.\n"
+ "-v Prints the detail of the response such as protocol, status and headers.\n"
+ "-h key:value Associates headers to HTTP Request with the format 'key:value'.";
case "post":
return "usage: httpc post [-v] [-h key:value] [-d inline-data] [-f file] URL \n"
+ "Post executes a HTTP POST request for a given URL with inline data or from file. \n"
+ "-v Prints the detail of the response such as protocol, status and headers. \n"
+ "-h key:value Associates headers to HTTP Request with the format 'key:value'.\n"
+ "-d string Associates an inline data to the body HTTP POST request.\n -f file Associates the content of a file to the body HTTP POST request.\n"
+ "Either [-d] or [-f] can be used but not both.";
default:
return " The help you are trying to get does not exist";
}
}
public static Boolean contains(String[] arguments, String character) {
for (String element : arguments) {
if (character.equals(element)) {
return true;
}
}
return false;
}
public static void setHeaders(String[] arguments) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equalsIgnoreCase("-h")) {
parameter.addHeader(arguments[i + 1] + "\r\n");
}
}
}
public static void setURL(String[] arguments) {
if (parameter.isOutputToFile()) {
parameter.setURL(arguments[arguments.length - 3]);
} else {
parameter.setURL(arguments[arguments.length - 1]);
}
parameter.setRequestType(arguments[0].toUpperCase() + " ");
}
public static void setBodyD(String[] arguments) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equalsIgnoreCase("-d")) {
parameter.setBody(arguments[i + 1]);
}
}
}
public static void setBodyF(String[] arguments) {
int index = 0;
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("-f")) {
index = i + 1;
break;
}
}
try {
// pass the path to the file as a parameter
File file = new File("C:\\Github\\Network\\" + arguments[index]);
Scanner sc = new Scanner(file);
String temp = "";
while (sc.hasNextLine()) {
temp += sc.nextLine();
}
parameter.setBody(temp);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e);
System.out.println("The file given does not exist");
System.exit(0);
}
}
public static void outputToFile(Client net, String outputFile, Boolean verbose) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Github\\Network\\"+outputFile));
if(verbose)
{
writer.write(net.getRes().verboseToString(false));
}
else {
writer.write(net.getRes().toString());
}
writer.close();
} catch (IOException e) {
System.out.println("Writing output to file has failed");
}
}
}