|
1 | 1 | package client; |
2 | 2 |
|
3 | | -import util.consts.WebMethods; |
| 3 | +import message.consts.WebMethods; |
4 | 4 |
|
5 | | -import java.io.IOException; |
| 5 | +import java.net.URI; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Locale; |
6 | 9 |
|
7 | 10 | public class ClientDriver { |
8 | | - public static void main(String[] args) { |
9 | | - String hostName = "www.baidu.com"; |
10 | | - int hostPort = 80; |
11 | | - boolean longConnection = false; |
| 11 | + public static final String HELP = """ |
| 12 | + SYNOPSIS |
| 13 | + ~ <URL> |
| 14 | + [-m <METHOD>] [--keep-alive] [-b <text>] |
| 15 | + [-h <headers>...] |
| 16 | + |
| 17 | + URL |
| 18 | + Using the generic URI syntax of: |
| 19 | + http://<HOSTNAME>[:PORT][/PATH][?QUERY] |
| 20 | + e.g.: http://jyywiki.cn/OS/2020/, http://127.0.0.1:8080/help/, http://www.google.com/search?q=uri |
| 21 | + The default value of the port number is 80. |
| 22 | + Only support HTTP protocol (not HTTPS). |
| 23 | + |
| 24 | + OPTIONS |
| 25 | + -m <METHOD> Send with the specified web method. |
| 26 | + Only supports GET and POST. |
| 27 | + The default value is GET. |
| 28 | + |
| 29 | + --keep-alive Enable keep-alive. |
| 30 | + |
| 31 | + -b <text> Plain text body. |
| 32 | + |
| 33 | + -h <header>... Send with the specified headers. |
| 34 | + Syntax: <key>:<value> |
| 35 | + e.g.: User-Agent:AbaAba/0.1 |
| 36 | + """; |
| 37 | + |
| 38 | + private static class InvalidCommandException extends Exception { |
| 39 | + public InvalidCommandException(String message) { |
| 40 | + super(message); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static class ArgIterator { |
| 45 | + private final |
| 46 | + String[] args; |
| 47 | + private |
| 48 | + int idx; |
| 49 | + |
| 50 | + public ArgIterator(String[] args) { |
| 51 | + this.args = args; |
| 52 | + this.idx = 0; |
| 53 | + for (String first; (first = peek()) != null && !first.startsWith("http://"); ) |
| 54 | + next(); |
| 55 | + |
| 56 | + } |
12 | 57 |
|
13 | | - String method = WebMethods.GET; |
14 | | - String target = "/"; |
15 | | - String param = null; |
16 | | - String body = null; |
| 58 | + public boolean hasNext() { |
| 59 | + return idx < args.length; |
| 60 | + } |
| 61 | + |
| 62 | + public String peek() { |
| 63 | + if (hasNext()) |
| 64 | + return args[idx]; |
| 65 | + else |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public String next() { |
| 70 | + if (hasNext()) |
| 71 | + return args[idx++]; |
| 72 | + else |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public String[] nextValues() { |
| 77 | + ArrayList<String> as = new ArrayList<>(); |
| 78 | + for (String token; (token = peek()) != null && !token.startsWith("-"); next()) |
| 79 | + as.add(token); |
| 80 | + return as.toArray(new String[0]); |
| 81 | + } |
| 82 | + } |
17 | 83 |
|
| 84 | + public static void main(String[] args) { |
18 | 85 | try { |
19 | | - HttpClient client = new HttpClient(hostName, hostPort, longConnection); |
20 | | - var hrm = client.request(method, target, param, body); |
| 86 | + ArgIterator ai = new ArgIterator(args); |
| 87 | + |
| 88 | + String raw = ai.next(); |
| 89 | + if (raw == null || raw.startsWith("-")) |
| 90 | + throw new InvalidCommandException("Lack of hostName"); |
| 91 | + |
| 92 | + URI u = new URI(raw); |
| 93 | + |
| 94 | + String hostName = u.getHost(); |
| 95 | + String query = u.getQuery(); |
| 96 | + int port = u.getPort(); |
| 97 | + String path = u.getPath(); |
| 98 | + |
| 99 | + if (port == -1) port = 80; |
| 100 | + if (path == null || path.length() == 0) path = "/"; |
| 101 | + |
| 102 | + String method = WebMethods.GET; |
| 103 | + boolean keepAlive = false; |
| 104 | + String body = null; |
| 105 | + String[] headers = new String[0]; |
| 106 | + |
| 107 | + while (ai.hasNext()) { |
| 108 | + String opt = ai.next(); |
| 109 | + if (opt == null) throw new InvalidCommandException("Invalid option"); |
| 110 | + switch (opt) { |
| 111 | + case "-m" -> { |
| 112 | + method = ai.next(); |
| 113 | + assert method != null; |
| 114 | + method = method.toUpperCase(Locale.ROOT); |
| 115 | + assert WebMethods.GET.equals(method) |
| 116 | + || WebMethods.POST.equals(method); |
| 117 | + } |
| 118 | + |
| 119 | + case "--keep-alive" -> { |
| 120 | + keepAlive = true; |
| 121 | + } |
| 122 | + |
| 123 | + case "-b" -> { |
| 124 | + body = ai.next(); |
| 125 | + } |
| 126 | + |
| 127 | + case "-h" -> { |
| 128 | + headers = ai.nextValues(); |
| 129 | + } |
| 130 | + |
| 131 | + default -> { |
| 132 | + throw new InvalidCommandException("Invalid token at \"%s\"".formatted(opt)); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + HttpClient client = new HttpClient(hostName, port, keepAlive); |
| 138 | + var hrm = client.request(method, u.toString(), body, headers); |
21 | 139 |
|
22 | 140 | System.out.println( |
23 | | - HttpClient.present(hrm) |
| 141 | + client.present(hrm) |
24 | 142 | ); |
25 | | - } catch (IOException e) { |
| 143 | + |
| 144 | + } catch (InvalidCommandException e) { |
| 145 | + System.err.println(Arrays.toString(args)); |
| 146 | + System.err.println("Error: " + e.getMessage() + "\n"); |
| 147 | + System.err.println(HELP); |
| 148 | + } catch (Exception e) { |
| 149 | + System.err.println(Arrays.toString(args)); |
26 | 150 | e.printStackTrace(); |
| 151 | + System.err.println(HELP); |
27 | 152 | } |
28 | 153 |
|
| 154 | + |
29 | 155 | } |
30 | 156 | } |
0 commit comments