33import java .io .BufferedReader ;
44import java .io .IOException ;
55import java .util .HashMap ;
6+ import java .util .function .Consumer ;
7+ import java .util .function .Function ;
8+ import java .util .function .Predicate ;
69
710public class RequestParser {
811 private String method = "" ;
912 private String path = "" ;
1013 private String httpVersion = "" ;
1114 private BufferedReader reader ;
15+ private HashMap <String , String > postData ;
1216 private HashMap <String , String > headers ;
13-
14-
17+
1518 public RequestParser (BufferedReader reader ) {
1619 this .reader = reader ;
20+ postData = new HashMap <String , String >();
1721 headers = new HashMap <>();
1822 }
1923
20- public void getHeaders (FilterData <String > filterData ) {
21- try {
22- while (reader .ready ()) {
23- String line = reader .readLine ();
24-
25- // if the filter has found a reason to stop continuing
26- if (filterData .badData (line , reader )) {
27- break ;
28- }
29-
30- String [] header = line .split (":" , 2 );
31-
32- System .out .println (line );
33-
34- if (header .length == 2 ) {
35- headers .put (header [0 ], header [1 ]);
36- }
37- }
38- } catch (IOException e ) {
39- e .printStackTrace ();
24+ public void parseRequest () {
25+ parseFirstLine ();
26+
27+ if (method .equals ("GET" )) {
28+ parseContentAfterFirstLineForGetRequest ();
29+ } else if (method .equals ("POST" )) {
30+ parseContentAfterFirstLineForPostRequest ();
4031 }
4132 }
4233
43- public void parseRequest () {
34+ public void parseFirstLine () {
4435 try {
36+
4537 boolean waitingForInformation = true ;
4638
4739 while (waitingForInformation ) {
4840 if (reader .ready ()) {
4941 waitingForInformation = false ;
5042
51- String [] firstLine = reader .readLine ().split (" " ); // should be the METHOD PATH HTTP VERSION
43+ String [] firstLine = reader .readLine ().split (" " );
5244
5345 method = firstLine [0 ];
5446 path = firstLine [1 ];
5547 httpVersion = firstLine [2 ];
5648 }
5749 }
5850
59- if (method .equals ("GET" )) {
60- getHeaders ((data , reader ) -> {
61- return true ;
51+ } catch (IOException e ) {
52+ e .printStackTrace ();
53+ }
54+ }
55+
56+ public void parseContentAfterFirstLineForGetRequest () {
57+ readContent (line -> false );
58+ }
59+
60+ public void parseContentAfterFirstLineForPostRequest () {
61+ readContent (line -> parseRequestLineForSignsOfPostData (line ));
62+ }
63+
64+ public void readContent (FilterData <String > filterData ) {
65+ try {
66+ while (reader .ready ()) {
67+ String line = reader .readLine ();
68+
69+ // If the filter found a reason to stop continuing reading data from the request
70+ if (filterData .badData (line )) {
71+ break ;
72+ }
73+
74+ splitLine (line , ":" , (splittedLine ) -> splittedLine .length == 2 , (splittedLine ) -> {
75+ headers .put (splittedLine [0 ], splittedLine [1 ]);
76+
77+ System .out .println (splittedLine [0 ] + ":" + splittedLine [1 ]);
6278 });
63- } else if (method .equals ("POST" )) {
64- getHeaders ((data , reader ) -> {
65- if (data .length () == 0 ) {
66- try {
67- while (reader .ready ())
68- System .out .println ("post data: " + reader .readLine ());
69- } catch (IOException e ) {
70- e .printStackTrace ();
71- }
72- return true ;
73- } else {
74- return false ;
79+
80+ }
81+ } catch (IOException e ) {
82+ e .printStackTrace ();
83+ }
84+ }
85+
86+ public void splitLine (String line , String splitter , Predicate <String []> predicate , Consumer <String []> action ) {
87+ String [] splitLine = line .split (splitter );
88+
89+ if (predicate .test (splitLine )) {
90+ action .accept (splitLine );
91+ }
92+ }
93+
94+ public boolean parseRequestLineForSignsOfPostData (String line ) {
95+ if (line .length () == 0 ) {
96+
97+ try {
98+ StringBuilder tempPostData = new StringBuilder ();
99+
100+ while (reader .ready ()) {
101+ tempPostData .append ((char )reader .read ());
102+ }
103+
104+ System .out .println (tempPostData );
105+
106+ splitLine (tempPostData .toString (), "&" , (splittedLine ) -> true , (splittedLine ) -> {
107+
108+ for (String parameterAndValue : splittedLine ) {
109+ String [] parameterSplitted = parameterAndValue .split ("=" );
110+ postData .put (parameterSplitted [0 ], parameterSplitted [1 ]);
111+ System .out .println (parameterSplitted [0 ] + "=" + parameterSplitted [1 ]);
75112 }
113+
76114 });
115+
116+ return true ; // Post data will be the next line
117+
118+ } catch (IOException e ) {
119+ e .printStackTrace ();
77120 }
78- } catch (IOException e ) {
79- e .printStackTrace ();
80121 }
122+
123+ return false ; // If post data has not been recognized
81124 }
82125
83126 public String getMethod () {
@@ -91,4 +134,12 @@ public String getPath() {
91134 public String getHttpVersion () {
92135 return httpVersion ;
93136 }
137+
138+ public HashMap <String , String > getPostData () {
139+ return postData ;
140+ }
141+
142+ public HashMap <String , String > getHeaders () {
143+ return headers ;
144+ }
94145}
0 commit comments