Mercurial Hosting > luan
comparison src/luan/webserver/RequestParser.java @ 1146:2dda3c92a473
webserver - implement cookies
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 01 Feb 2018 03:08:21 -0700 |
parents | 12ececf30597 |
children | 30d87b7d1d62 |
comparison
equal
deleted
inserted
replaced
1145:12ececf30597 | 1146:2dda3c92a473 |
---|---|
1 package luan.webserver; | 1 package luan.webserver; |
2 | 2 |
3 import java.io.UnsupportedEncodingException; | |
4 import java.net.URLDecoder; | |
5 import java.util.List; | 3 import java.util.List; |
6 import java.util.ArrayList; | 4 import java.util.ArrayList; |
7 import luan.lib.parser.Parser; | 5 import luan.lib.parser.Parser; |
8 import luan.lib.parser.ParseException; | 6 import luan.lib.parser.ParseException; |
9 | 7 |
26 this.parser = new Parser(request.rawHead); | 24 this.parser = new Parser(request.rawHead); |
27 parseRequestLine(); | 25 parseRequestLine(); |
28 while( !parser.match("\r\n") ) { | 26 while( !parser.match("\r\n") ) { |
29 parserHeaderField(); | 27 parserHeaderField(); |
30 } | 28 } |
29 parseCookies(); | |
31 } | 30 } |
32 | 31 |
33 private void parseRequestLine() throws ParseException { | 32 private void parseRequestLine() throws ParseException { |
34 parseMethod(); | 33 parseMethod(); |
35 require( parser.match(' ') ); | 34 require( parser.match(' ') ); |
62 private void parsePath() throws ParseException { | 61 private void parsePath() throws ParseException { |
63 int start = parser.currentIndex(); | 62 int start = parser.currentIndex(); |
64 if( !parser.match('/') ) | 63 if( !parser.match('/') ) |
65 throw new ParseException(parser,"bad path"); | 64 throw new ParseException(parser,"bad path"); |
66 while( safePathChar() || parser.anyOf("&=") ); | 65 while( safePathChar() || parser.anyOf("&=") ); |
67 request.path = decode( parser.textFrom(start) ); | 66 request.path = Util.urlDecode( parser.textFrom(start) ); |
68 } | 67 } |
69 | 68 |
70 private void parseQuery() throws ParseException { | 69 private void parseQuery() throws ParseException { |
71 while(true) { | 70 while(true) { |
72 while( parser.match('&') ); | 71 while( parser.match('&') ); |
73 int start = parser.currentIndex(); | 72 int start = parser.currentIndex(); |
74 if( !queryChar() ) | 73 if( !queryChar() ) |
75 return; | 74 return; |
76 while( queryChar() ); | 75 while( queryChar() ); |
77 String name = decode( parser.textFrom(start) ); | 76 String name = Util.urlDecode( parser.textFrom(start) ); |
78 String value; | 77 String value; |
79 if( parser.match('=') ) { | 78 if( parser.match('=') ) { |
80 start = parser.currentIndex(); | 79 start = parser.currentIndex(); |
81 while( queryChar() ); | 80 while( queryChar() ); |
82 value = decode( parser.textFrom(start) ); | 81 value = Util.urlDecode( parser.textFrom(start) ); |
83 } else { | 82 } else { |
84 value = ""; | 83 value = ""; |
85 } | 84 } |
86 Object current = request.parameters.get(name); | 85 Util.add(request.parameters,name,value); |
87 if( current == null ) { | |
88 request.parameters.put(name,value); | |
89 } else if( current instanceof List ) { | |
90 List list = (List)current; | |
91 list.add(value); | |
92 } else { | |
93 List list = new ArrayList(); | |
94 list.add(current); | |
95 list.add(value); | |
96 request.parameters.put(name,list); | |
97 } | |
98 } | 86 } |
99 } | 87 } |
100 | 88 |
101 private boolean queryChar() { | 89 private boolean queryChar() { |
102 return safePathChar() || parser.anyOf("?"); | 90 return safePathChar() || parser.anyOf("?"); |
129 require( parser.match(':') ); | 117 require( parser.match(':') ); |
130 while( parser.anyOf(" \t") ); | 118 while( parser.anyOf(" \t") ); |
131 String value = parseValue(); | 119 String value = parseValue(); |
132 while( parser.anyOf(" \t") ); | 120 while( parser.anyOf(" \t") ); |
133 require( parser.match("\r\n") ); | 121 require( parser.match("\r\n") ); |
134 request.headers.put(name,value); | 122 Util.add(request.headers,name,value); |
135 } | 123 } |
136 | 124 |
137 private String parseName() throws ParseException { | 125 private String parseName() throws ParseException { |
138 StringBuilder buf = new StringBuilder(); | 126 StringBuilder buf = new StringBuilder(); |
139 boolean cap = true; | 127 boolean cap = true; |
183 } else { | 171 } else { |
184 return false; | 172 return false; |
185 } | 173 } |
186 } | 174 } |
187 | 175 |
188 private static String decode(String s) { | 176 |
189 try { | 177 private void parseCookies() throws ParseException { |
190 return URLDecoder.decode(s,"UTF-8"); | 178 String text = (String)request.headers.get("Cookie"); |
191 } catch(UnsupportedEncodingException e) { | 179 if( text == null ) |
192 throw new RuntimeException(e); | 180 return; |
181 this.parser = new Parser(text); | |
182 while(true) { | |
183 int start = parser.currentIndex(); | |
184 while( parser.noneOf("=;") ); | |
185 String name = Util.urlDecode( parser.textFrom(start) ); | |
186 if( parser.match('=') ) { | |
187 start = parser.currentIndex(); | |
188 while( parser.noneOf(";") ); | |
189 String value = Util.urlDecode( parser.textFrom(start) ); | |
190 request.cookies.put(name,value); | |
191 } | |
192 if( parser.endOfInput() ) | |
193 return; | |
194 require( parser.match("; ") ); | |
193 } | 195 } |
194 } | 196 } |
197 | |
195 } | 198 } |