3
3
import dev .latvian .apps .tinyserver .CompiledPath ;
4
4
import dev .latvian .apps .tinyserver .HTTPServer ;
5
5
import dev .latvian .apps .tinyserver .error .InvalidPathException ;
6
+ import dev .latvian .apps .tinyserver .http .response .HTTPResponse ;
7
+ import dev .latvian .apps .tinyserver .http .response .HTTPResponseBuilder ;
6
8
7
9
import java .io .IOException ;
8
10
import java .io .InputStream ;
14
16
15
17
public class HTTPRequest {
16
18
private HTTPServer <?> server ;
17
- private String [] path = new String [0 ];
19
+ private String path = "" ;
20
+ private String [] pathParts = new String [0 ];
18
21
private Map <String , String > variables = Map .of ();
22
+ private String queryString = "" ;
19
23
private Map <String , String > query = Map .of ();
20
24
private List <Header > headers = List .of ();
21
25
private InputStream bodyStream = null ;
22
26
23
- public void init (HTTPServer <?> server , String [] path , CompiledPath compiledPath , List <Header > headers , Map <String , String > query , InputStream bodyStream ) {
27
+ public void init (HTTPServer <?> server , String path , String [] pathParts , CompiledPath compiledPath , List <Header > headers , String queryString , Map <String , String > query , InputStream bodyStream ) {
24
28
this .server = server ;
25
29
this .path = path ;
30
+ this .pathParts = pathParts ;
26
31
27
32
if (compiledPath .variables () > 0 ) {
28
33
this .variables = new HashMap <>(compiledPath .variables ());
29
34
30
35
for (var i = 0 ; i < compiledPath .parts ().length ; i ++) {
31
36
var part = compiledPath .parts ()[i ];
32
37
33
- if (part .variable () && i < path .length ) {
34
- variables .put (part .name (), path [i ]);
38
+ if (part .variable () && i < pathParts .length ) {
39
+ variables .put (part .name (), pathParts [i ]);
35
40
}
36
41
}
37
42
}
38
43
39
44
this .headers = headers ;
45
+ this .queryString = queryString ;
40
46
this .query = query ;
41
47
this .bodyStream = bodyStream ;
42
48
}
@@ -59,6 +65,10 @@ public String variable(String name) {
59
65
return s ;
60
66
}
61
67
68
+ public String queryString () {
69
+ return queryString ;
70
+ }
71
+
62
72
public Map <String , String > query () {
63
73
return query ;
64
74
}
@@ -77,10 +87,18 @@ public String header(String name) {
77
87
return "" ;
78
88
}
79
89
80
- public String [] path () {
90
+ public String path () {
81
91
return path ;
82
92
}
83
93
94
+ public String fullPath () {
95
+ return path + (queryString .isEmpty () ? "" : "?" + queryString );
96
+ }
97
+
98
+ public String [] pathParts () {
99
+ return pathParts ;
100
+ }
101
+
84
102
public InputStream bodyStream () {
85
103
if (bodyStream == null ) {
86
104
return InputStream .nullInputStream ();
@@ -103,4 +121,10 @@ public byte[] bodyBytes() throws IOException {
103
121
public String body () throws IOException {
104
122
return new String (bodyBytes (), StandardCharsets .UTF_8 );
105
123
}
124
+
125
+ public void handlePayloadError (HTTPResponseBuilder payload , Exception error ) {
126
+ }
127
+
128
+ public void afterResponse (HTTPResponseBuilder payload , HTTPResponse response ) {
129
+ }
106
130
}
0 commit comments