1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.UnsupportedEncodingException;
23 import java.net.InetAddress;
24 import java.nio.ByteBuffer;
25 import java.security.Principal;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.EventListener;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35
36 import javax.servlet.RequestDispatcher;
37 import javax.servlet.ServletContext;
38 import javax.servlet.ServletInputStream;
39 import javax.servlet.ServletRequestAttributeEvent;
40 import javax.servlet.ServletRequestAttributeListener;
41 import javax.servlet.ServletRequestWrapper;
42 import javax.servlet.ServletResponse;
43 import javax.servlet.http.Cookie;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpSession;
46
47 import org.mortbay.io.Buffer;
48 import org.mortbay.io.BufferUtil;
49 import org.mortbay.io.EndPoint;
50 import org.mortbay.io.Portable;
51 import org.mortbay.io.nio.DirectNIOBuffer;
52 import org.mortbay.io.nio.IndirectNIOBuffer;
53 import org.mortbay.io.nio.NIOBuffer;
54 import org.mortbay.jetty.handler.ContextHandler;
55 import org.mortbay.jetty.handler.ContextHandler.SContext;
56 import org.mortbay.jetty.security.Authenticator;
57 import org.mortbay.jetty.security.SecurityHandler;
58 import org.mortbay.jetty.security.UserRealm;
59 import org.mortbay.log.Log;
60 import org.mortbay.util.Attributes;
61 import org.mortbay.util.AttributesMap;
62 import org.mortbay.util.LazyList;
63 import org.mortbay.util.MultiMap;
64 import org.mortbay.util.StringUtil;
65 import org.mortbay.util.URIUtil;
66 import org.mortbay.util.UrlEncoded;
67 import org.mortbay.util.ajax.Continuation;
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 public class Request implements HttpServletRequest
103 {
104 private static final byte STATE_DELIMITER = 1;
105 private static final byte STATE_NAME = 2;
106 private static final byte STATE_VALUE = 4;
107 private static final byte STATE_QUOTED_VALUE = 8;
108 private static final byte STATE_UNQUOTED_VALUE = 16;
109
110 private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault());
111 private static final int __NONE=0, _STREAM=1, __READER=2;
112
113 private boolean _handled =false;
114 private HttpConnection _connection;
115 private EndPoint _endp;
116 private Map _roleMap;
117
118 private Attributes _attributes;
119 private String _authType;
120 private String _characterEncoding;
121 private String _queryEncoding;
122 private String _serverName;
123 private String _remoteAddr;
124 private String _remoteHost;
125 private String _method;
126 private String _pathInfo;
127 private int _port;
128 private String _protocol=HttpVersions.HTTP_1_1;
129 private String _queryString;
130 private String _requestedSessionId;
131 private boolean _requestedSessionIdFromCookie=false;
132 private String _requestURI;
133 private String _scheme=URIUtil.HTTP;
134 private String _contextPath;
135 private String _servletPath;
136 private String _servletName;
137 private HttpURI _uri;
138 private Principal _userPrincipal;
139 private MultiMap _parameters;
140 private MultiMap _baseParameters;
141 private boolean _paramsExtracted;
142 private int _inputState=__NONE;
143 private BufferedReader _reader;
144 private String _readerEncoding;
145 private boolean _dns=false;
146 private ContextHandler.SContext _context;
147 private HttpSession _session;
148 private SessionManager _sessionManager;
149 private boolean _cookiesExtracted=false;
150 private Cookie[] _cookies;
151 private String[] _lastCookies;
152 private long _timeStamp;
153 private Buffer _timeStampBuffer;
154 private Continuation _continuation;
155 private Object _requestAttributeListeners;
156 private Map _savedNewSessions;
157 private UserRealm _userRealm;
158
159
160
161
162
163 public Request()
164 {
165 }
166
167
168
169
170
171 public Request(HttpConnection connection)
172 {
173 _connection=connection;
174 _endp=connection.getEndPoint();
175 _dns=_connection.getResolveNames();
176 }
177
178
179 protected void setConnection(HttpConnection connection)
180 {
181 _connection=connection;
182 _endp=connection.getEndPoint();
183 _dns=connection.getResolveNames();
184 }
185
186
187 protected void recycle()
188 {
189 _handled=false;
190 if (_context!=null)
191 throw new IllegalStateException("Request in context!");
192 if(_attributes!=null)
193 _attributes.clearAttributes();
194 _authType=null;
195 _characterEncoding=null;
196 _queryEncoding=null;
197 _context=null;
198 _serverName=null;
199 _method=null;
200 _pathInfo=null;
201 _port=0;
202 _protocol=HttpVersions.HTTP_1_1;
203 _queryString=null;
204 _requestedSessionId=null;
205 _requestedSessionIdFromCookie=false;
206 _session=null;
207 _requestURI=null;
208 _scheme=URIUtil.HTTP;
209 _servletPath=null;
210 _timeStamp=0;
211 _timeStampBuffer=null;
212 _uri=null;
213 _userPrincipal=null;
214 if (_baseParameters!=null)
215 _baseParameters.clear();
216 _parameters=null;
217 _paramsExtracted=false;
218 _inputState=__NONE;
219
220 _cookiesExtracted=false;
221 if (_savedNewSessions!=null)
222 _savedNewSessions.clear();
223 _savedNewSessions=null;
224 if (_continuation!=null && _continuation.isPending())
225 _continuation.reset();
226 }
227
228
229
230
231
232
233
234 public Buffer getTimeStampBuffer()
235 {
236 if (_timeStampBuffer == null && _timeStamp > 0)
237 _timeStampBuffer = HttpFields.__dateCache.formatBuffer(_timeStamp);
238 return _timeStampBuffer;
239 }
240
241
242
243
244
245
246
247 public long getTimeStamp()
248 {
249 return _timeStamp;
250 }
251
252
253 public void setTimeStamp(long ts)
254 {
255 _timeStamp = ts;
256 }
257
258
259 public boolean isHandled()
260 {
261 return _handled;
262 }
263
264
265 public void setHandled(boolean h)
266 {
267 _handled=h;
268 }
269
270
271
272
273
274
275 public Object getAttribute(String name)
276 {
277 if ("org.mortbay.jetty.ajax.Continuation".equals(name))
278 return getContinuation(true);
279
280 if (_attributes==null)
281 return null;
282 return _attributes.getAttribute(name);
283 }
284
285
286
287
288
289 public Enumeration getAttributeNames()
290 {
291 if (_attributes==null)
292 return Collections.enumeration(Collections.EMPTY_LIST);
293 return AttributesMap.getAttributeNamesCopy(_attributes);
294 }
295
296
297
298
299
300 public String getAuthType()
301 {
302 return _authType;
303 }
304
305
306
307
308
309 public String getCharacterEncoding()
310 {
311 return _characterEncoding;
312 }
313
314 public long getContentRead()
315 {
316 if (_connection==null || _connection.getParser()==null)
317 return -1;
318
319 return ((HttpParser)_connection.getParser()).getContentRead();
320 }
321
322
323
324
325
326 public int getContentLength()
327 {
328 return (int)_connection.getRequestFields().getLongField(HttpHeaders.CONTENT_LENGTH_BUFFER);
329 }
330
331
332
333
334
335 public String getContentType()
336 {
337 return _connection.getRequestFields().getStringField(HttpHeaders.CONTENT_TYPE_BUFFER);
338 }
339
340
341
342
343
344 public void setContentType(String contentType)
345 {
346 _connection.getRequestFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,contentType);
347
348 }
349
350
351
352
353
354 public String getContextPath()
355 {
356 return _contextPath;
357 }
358
359
360
361
362
363 public Cookie[] getCookies()
364 {
365 if (_cookiesExtracted) return _cookies;
366
367 try
368 {
369
370 if (!_connection.getRequestFields().containsKey(HttpHeaders.COOKIE_BUFFER))
371 {
372 _cookies = null;
373 _cookiesExtracted = true;
374 _lastCookies = null;
375 return _cookies;
376 }
377
378
379 if (_lastCookies != null)
380 {
381 int last = 0;
382 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.COOKIE_BUFFER);
383 while (enm.hasMoreElements())
384 {
385 String c = (String)enm.nextElement();
386 if (last >= _lastCookies.length || !c.equals(_lastCookies[last]))
387 {
388 _lastCookies = null;
389 break;
390 }
391 last++;
392 }
393 if (_lastCookies != null && _lastCookies.length==last)
394 {
395 _cookiesExtracted = true;
396 return _cookies;
397 }
398 }
399
400
401 Object cookies = null;
402 Object lastCookies = null;
403
404 int version = 0;
405
406
407 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.COOKIE_BUFFER);
408 while (enm.hasMoreElements())
409 {
410
411 String hdr = (String)enm.nextElement();
412 lastCookies = LazyList.add(lastCookies, hdr);
413
414
415 String name = null;
416 String value = null;
417
418 Cookie cookie = null;
419
420 byte state = STATE_NAME;
421 for (int i = 0, tokenstart = 0, length = hdr.length(); i < length; i++)
422 {
423 char c = hdr.charAt(i);
424 switch (c)
425 {
426 case ',':
427 case ';':
428 switch (state)
429 {
430 case STATE_DELIMITER:
431 state = STATE_NAME;
432 tokenstart = i + 1;
433 break;
434 case STATE_UNQUOTED_VALUE:
435 state = STATE_NAME;
436 value = hdr.substring(tokenstart, i).trim();
437 if(isRequestedSessionIdFromURL())
438 value = URIUtil.decodePath(value);
439 tokenstart = i + 1;
440 break;
441 case STATE_NAME:
442 name = hdr.substring(tokenstart, i);
443 value = "";
444 tokenstart = i + 1;
445 break;
446 case STATE_VALUE:
447 state = STATE_NAME;
448 value = "";
449 tokenstart = i + 1;
450 break;
451 }
452 break;
453 case '=':
454 switch (state)
455 {
456 case STATE_NAME:
457 state = STATE_VALUE;
458 name = hdr.substring(tokenstart, i);
459 tokenstart = i + 1;
460 break;
461 case STATE_VALUE:
462 state = STATE_UNQUOTED_VALUE;
463 tokenstart = i;
464 break;
465 }
466 break;
467 case '"':
468 switch (state)
469 {
470 case STATE_VALUE:
471 state = STATE_QUOTED_VALUE;
472 tokenstart = i + 1;
473 break;
474 case STATE_QUOTED_VALUE:
475 state = STATE_DELIMITER;
476 value = hdr.substring(tokenstart, i);
477 break;
478 }
479 break;
480 case ' ':
481 case '\t':
482 break;
483 default:
484 switch (state)
485 {
486 case STATE_VALUE:
487 state = STATE_UNQUOTED_VALUE;
488 tokenstart = i;
489 break;
490 case STATE_DELIMITER:
491 state = STATE_NAME;
492 tokenstart = i;
493 break;
494 }
495 }
496
497 if (i + 1 == length)
498 {
499 switch (state)
500 {
501 case STATE_UNQUOTED_VALUE:
502 value = hdr.substring(tokenstart).trim();
503 if(isRequestedSessionIdFromURL())
504 value = URIUtil.decodePath(value);
505 break;
506 case STATE_NAME:
507 name = hdr.substring(tokenstart);
508 value = "";
509 break;
510 case STATE_VALUE:
511 value = "";
512 break;
513 }
514 }
515
516 if (name != null && value != null)
517 {
518 name = name.trim();
519
520 try
521 {
522 if (name.startsWith("$"))
523 {
524 String lowercaseName = name.toLowerCase();
525 if ("$path".equals(lowercaseName))
526 {
527 cookie.setPath(value);
528 }
529 else if ("$domain".equals(lowercaseName))
530 {
531 cookie.setDomain(value);
532 }
533 else if ("$version".equals(lowercaseName))
534 {
535 version = Integer.parseInt(value);
536 }
537 }
538 else
539 {
540 cookie = new Cookie(name, value);
541
542 if (version > 0)
543 {
544 cookie.setVersion(version);
545 }
546
547 cookies = LazyList.add(cookies, cookie);
548 }
549 }
550 catch (Exception e)
551 {
552 Log.ignore(e);
553 }
554
555 name = null;
556 value = null;
557 }
558 }
559 }
560
561 int l = LazyList.size(cookies);
562 _cookiesExtracted = true;
563 if (l>0)
564 {
565 if (_cookies == null || _cookies.length != l) _cookies = new Cookie[l];
566 for (int i = 0; i < l; i++)
567 _cookies[i] = (Cookie) LazyList.get(cookies, i);
568
569 l = LazyList.size(lastCookies);
570 _lastCookies = new String[l];
571 for (int i = 0; i < l; i++)
572 _lastCookies[i] = (String) LazyList.get(lastCookies, i);
573 }
574 }
575 catch (Exception e)
576 {
577 Log.warn(e);
578 }
579
580 if (_cookies==null || _cookies.length==0)
581 return null;
582 return _cookies;
583 }
584
585
586
587
588
589 public long getDateHeader(String name)
590 {
591 return _connection.getRequestFields().getDateField(name);
592 }
593
594
595
596
597
598 public String getHeader(String name)
599 {
600 return _connection.getRequestFields().getStringField(name);
601 }
602
603
604
605
606
607 public Enumeration getHeaderNames()
608 {
609 return _connection.getRequestFields().getFieldNames();
610 }
611
612
613
614
615
616 public Enumeration getHeaders(String name)
617 {
618 Enumeration e = _connection.getRequestFields().getValues(name);
619 if (e==null)
620 return Collections.enumeration(Collections.EMPTY_LIST);
621 return e;
622 }
623
624
625
626
627
628 public ServletInputStream getInputStream() throws IOException
629 {
630 if (_inputState!=__NONE && _inputState!=_STREAM)
631 throw new IllegalStateException("READER");
632 _inputState=_STREAM;
633 return _connection.getInputStream();
634 }
635
636
637
638
639
640 public int getIntHeader(String name)
641 {
642 return (int)_connection.getRequestFields().getLongField(name);
643 }
644
645
646
647
648
649 public String getLocalAddr()
650 {
651 return _endp==null?null:_endp.getLocalAddr();
652 }
653
654
655
656
657
658 public Locale getLocale()
659 {
660 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
661
662
663 if (enm == null || !enm.hasMoreElements())
664 return Locale.getDefault();
665
666
667 List acceptLanguage = HttpFields.qualityList(enm);
668 if (acceptLanguage.size()==0)
669 return Locale.getDefault();
670
671 int size=acceptLanguage.size();
672
673
674 for (int i=0; i<size; i++)
675 {
676 String language = (String)acceptLanguage.get(i);
677 language=HttpFields.valueParameters(language,null);
678 String country = "";
679 int dash = language.indexOf('-');
680 if (dash > -1)
681 {
682 country = language.substring(dash + 1).trim();
683 language = language.substring(0,dash).trim();
684 }
685 return new Locale(language,country);
686 }
687
688 return Locale.getDefault();
689 }
690
691
692
693
694
695 public Enumeration getLocales()
696 {
697
698 Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.ACCEPT_LANGUAGE, HttpFields.__separators);
699
700
701 if (enm == null || !enm.hasMoreElements())
702 return Collections.enumeration(__defaultLocale);
703
704
705 List acceptLanguage = HttpFields.qualityList(enm);
706
707 if (acceptLanguage.size()==0)
708 return
709 Collections.enumeration(__defaultLocale);
710
711 Object langs = null;
712 int size=acceptLanguage.size();
713
714
715 for (int i=0; i<size; i++)
716 {
717 String language = (String)acceptLanguage.get(i);
718 language=HttpFields.valueParameters(language,null);
719 String country = "";
720 int dash = language.indexOf('-');
721 if (dash > -1)
722 {
723 country = language.substring(dash + 1).trim();
724 language = language.substring(0,dash).trim();
725 }
726 langs=LazyList.ensureSize(langs,size);
727 langs=LazyList.add(langs,new Locale(language,country));
728 }
729
730 if (LazyList.size(langs)==0)
731 return Collections.enumeration(__defaultLocale);
732
733 return Collections.enumeration(LazyList.getList(langs));
734 }
735
736
737
738
739
740 public String getLocalName()
741 {
742 if (_dns)
743 return _endp==null?null:_endp.getLocalHost();
744 return _endp==null?null:_endp.getLocalAddr();
745 }
746
747
748
749
750
751 public int getLocalPort()
752 {
753 return _endp==null?0:_endp.getLocalPort();
754 }
755
756
757
758
759
760 public String getMethod()
761 {
762 return _method;
763 }
764
765
766
767
768
769 public String getParameter(String name)
770 {
771 if (!_paramsExtracted)
772 extractParameters();
773 return (String) _parameters.getValue(name, 0);
774 }
775
776
777
778
779
780 public Map getParameterMap()
781 {
782 if (!_paramsExtracted)
783 extractParameters();
784
785 return Collections.unmodifiableMap(_parameters.toStringArrayMap());
786 }
787
788
789
790
791
792 public Enumeration getParameterNames()
793 {
794 if (!_paramsExtracted)
795 extractParameters();
796 return Collections.enumeration(_parameters.keySet());
797 }
798
799
800
801
802
803 public String[] getParameterValues(String name)
804 {
805 if (!_paramsExtracted)
806 extractParameters();
807 List vals = _parameters.getValues(name);
808 if (vals==null)
809 return null;
810 return (String[])vals.toArray(new String[vals.size()]);
811 }
812
813
814
815
816
817 public String getPathInfo()
818 {
819 return _pathInfo;
820 }
821
822
823
824
825
826 public String getPathTranslated()
827 {
828 if (_pathInfo==null || _context==null)
829 return null;
830 return _context.getRealPath(_pathInfo);
831 }
832
833
834
835
836
837 public String getProtocol()
838 {
839 return _protocol;
840 }
841
842
843
844
845
846 public BufferedReader getReader() throws IOException
847 {
848 if (_inputState!=__NONE && _inputState!=__READER)
849 throw new IllegalStateException("STREAMED");
850
851 if (_inputState==__READER)
852 return _reader;
853
854 String encoding=getCharacterEncoding();
855 if (encoding==null)
856 encoding=StringUtil.__ISO_8859_1;
857
858 if (_reader==null || !encoding.equalsIgnoreCase(_readerEncoding))
859 {
860 final ServletInputStream in = getInputStream();
861 _readerEncoding=encoding;
862 _reader=new BufferedReader(new InputStreamReader(in,encoding))
863 {
864 public void close() throws IOException
865 {
866 in.close();
867 }
868 };
869 }
870 _inputState=__READER;
871 return _reader;
872 }
873
874
875
876
877
878 public String getRealPath(String path)
879 {
880 if (_context==null)
881 return null;
882 return _context.getRealPath(path);
883 }
884
885
886
887
888
889 public String getRemoteAddr()
890 {
891 if (_remoteAddr != null)
892 return _remoteAddr;
893 return _endp==null?null:_endp.getRemoteAddr();
894 }
895
896
897
898
899
900 public String getRemoteHost()
901 {
902 if (_dns)
903 {
904 if (_remoteHost != null)
905 {
906 return _remoteHost;
907 }
908 return _endp==null?null:_endp.getRemoteHost();
909 }
910 return getRemoteAddr();
911 }
912
913
914
915
916
917 public int getRemotePort()
918 {
919 return _endp==null?0:_endp.getRemotePort();
920 }
921
922
923
924
925
926 public String getRemoteUser()
927 {
928 Principal p = getUserPrincipal();
929 if (p==null)
930 return null;
931 return p.getName();
932 }
933
934
935
936
937
938 public RequestDispatcher getRequestDispatcher(String path)
939 {
940 if (path == null || _context==null)
941 return null;
942
943
944 if (!path.startsWith("/"))
945 {
946 String relTo=URIUtil.addPaths(_servletPath,_pathInfo);
947 int slash=relTo.lastIndexOf("/");
948 if (slash>1)
949 relTo=relTo.substring(0,slash+1);
950 else
951 relTo="/";
952 path=URIUtil.addPaths(relTo,path);
953 }
954
955 return _context.getRequestDispatcher(path);
956 }
957
958
959
960
961
962 public String getRequestedSessionId()
963 {
964 return _requestedSessionId;
965 }
966
967
968
969
970
971 public String getRequestURI()
972 {
973 if (_requestURI==null && _uri!=null)
974 _requestURI=_uri.getPathAndParam();
975 return _requestURI;
976 }
977
978
979
980
981
982 public StringBuffer getRequestURL()
983 {
984 StringBuffer url = new StringBuffer(48);
985 synchronized (url)
986 {
987 String scheme = getScheme();
988 int port = getServerPort();
989
990 url.append(scheme);
991 url.append("://");
992 url.append(getServerName());
993 if (_port>0 &&
994 ((scheme.equalsIgnoreCase(URIUtil.HTTP) && port != 80) ||
995 (scheme.equalsIgnoreCase(URIUtil.HTTPS) && port != 443)))
996 {
997 url.append(':');
998 url.append(_port);
999 }
1000
1001 url.append(getRequestURI());
1002 return url;
1003 }
1004 }
1005
1006
1007
1008
1009
1010 public String getScheme()
1011 {
1012 return _scheme;
1013 }
1014
1015
1016
1017
1018
1019 public String getServerName()
1020 {
1021
1022 if (_serverName != null)
1023 return _serverName;
1024
1025
1026 _serverName = _uri.getHost();
1027 _port = _uri.getPort();
1028 if (_serverName != null)
1029 return _serverName;
1030
1031
1032 Buffer hostPort = _connection.getRequestFields().get(HttpHeaders.HOST_BUFFER);
1033 if (hostPort!=null)
1034 {
1035 for (int i=hostPort.length();i-->0;)
1036 {
1037 if (hostPort.peek(hostPort.getIndex()+i)==':')
1038 {
1039 _serverName=BufferUtil.to8859_1_String(hostPort.peek(hostPort.getIndex(), i));
1040 _port=BufferUtil.toInt(hostPort.peek(hostPort.getIndex()+i+1, hostPort.length()-i-1));
1041 return _serverName;
1042 }
1043 }
1044 if (_serverName==null || _port<0)
1045 {
1046 _serverName=BufferUtil.to8859_1_String(hostPort);
1047 _port = 0;
1048 }
1049
1050 return _serverName;
1051 }
1052
1053
1054 if (_connection != null)
1055 {
1056 _serverName = getLocalName();
1057 _port = getLocalPort();
1058 if (_serverName != null && !Portable.ALL_INTERFACES.equals(_serverName))
1059 return _serverName;
1060 }
1061
1062
1063 try
1064 {
1065 _serverName = InetAddress.getLocalHost().getHostAddress();
1066 }
1067 catch (java.net.UnknownHostException e)
1068 {
1069 Log.ignore(e);
1070 }
1071 return _serverName;
1072 }
1073
1074
1075
1076
1077
1078 public int getServerPort()
1079 {
1080 if (_port<=0)
1081 {
1082 if (_serverName==null)
1083 getServerName();
1084
1085 if (_port<=0)
1086 {
1087 if (_serverName!=null && _uri!=null)
1088 _port = _uri.getPort();
1089 else
1090 _port = _endp==null?0:_endp.getLocalPort();
1091 }
1092 }
1093
1094 if (_port<=0)
1095 {
1096 if (getScheme().equalsIgnoreCase(URIUtil.HTTPS))
1097 return 443;
1098 return 80;
1099 }
1100 return _port;
1101 }
1102
1103
1104
1105
1106
1107 public String getServletPath()
1108 {
1109 if (_servletPath==null)
1110 _servletPath="";
1111 return _servletPath;
1112 }
1113
1114
1115
1116
1117 public String getServletName()
1118 {
1119 return _servletName;
1120 }
1121
1122
1123
1124
1125
1126 public HttpSession getSession()
1127 {
1128 return getSession(true);
1129 }
1130
1131
1132
1133
1134
1135 public HttpSession getSession(boolean create)
1136 {
1137 if (_sessionManager==null && create)
1138 throw new IllegalStateException("No SessionHandler or SessionManager");
1139
1140 if (_session != null && _sessionManager!=null && _sessionManager.isValid(_session))
1141 return _session;
1142
1143 _session=null;
1144
1145 String id=getRequestedSessionId();
1146
1147 if (id != null && _sessionManager!=null)
1148 {
1149 _session=_sessionManager.getHttpSession(id);
1150 if (_session == null && !create)
1151 return null;
1152 }
1153
1154 if (_session == null && _sessionManager!=null && create )
1155 {
1156 _session=_sessionManager.newHttpSession(this);
1157 Cookie cookie=_sessionManager.getSessionCookie(_session,getContextPath(),isSecure());
1158 if (cookie!=null)
1159 _connection.getResponse().addCookie(cookie);
1160 }
1161
1162 return _session;
1163 }
1164
1165
1166
1167
1168
1169 public Principal getUserPrincipal()
1170 {
1171 if (_userPrincipal != null && _userPrincipal instanceof SecurityHandler.NotChecked)
1172 {
1173 SecurityHandler.NotChecked not_checked=(SecurityHandler.NotChecked)_userPrincipal;
1174 _userPrincipal = SecurityHandler.__NO_USER;
1175
1176 Authenticator auth=not_checked.getSecurityHandler().getAuthenticator();
1177 UserRealm realm=not_checked.getSecurityHandler().getUserRealm();
1178 String pathInContext=getPathInfo()==null?getServletPath():(getServletPath()+getPathInfo());
1179
1180 if (realm != null && auth != null)
1181 {
1182 try
1183 {
1184 auth.authenticate(realm, pathInContext, this, null);
1185 }
1186 catch (Exception e)
1187 {
1188 Log.ignore(e);
1189 }
1190 }
1191 }
1192
1193 if (_userPrincipal == SecurityHandler.__NO_USER)
1194 return null;
1195 return _userPrincipal;
1196 }
1197
1198
1199
1200
1201
1202 public String getQueryString()
1203 {
1204 if (_queryString==null && _uri!=null)
1205 {
1206 if (_queryEncoding==null)
1207 _queryString=_uri.getQuery();
1208 else
1209 _queryString=_uri.getQuery(_queryEncoding);
1210 }
1211 return _queryString;
1212 }
1213
1214
1215
1216
1217
1218 public boolean isRequestedSessionIdFromCookie()
1219 {
1220 return _requestedSessionId!=null && _requestedSessionIdFromCookie;
1221 }
1222
1223
1224
1225
1226
1227 public boolean isRequestedSessionIdFromUrl()
1228 {
1229 return _requestedSessionId!=null && !_requestedSessionIdFromCookie;
1230 }
1231
1232
1233
1234
1235
1236 public boolean isRequestedSessionIdFromURL()
1237 {
1238 return _requestedSessionId!=null && !_requestedSessionIdFromCookie;
1239 }
1240
1241
1242
1243
1244
1245 public boolean isRequestedSessionIdValid()
1246 {
1247 if (_requestedSessionId==null)
1248 return false;
1249
1250 HttpSession session=getSession(false);
1251 return (session==null?false:_sessionManager.getIdManager().getClusterId(_requestedSessionId).equals(_sessionManager.getClusterId(session)));
1252 }
1253
1254
1255
1256
1257
1258 public boolean isSecure()
1259 {
1260 return _connection.isConfidential(this);
1261 }
1262
1263
1264
1265
1266
1267 public boolean isUserInRole(String role)
1268 {
1269 if (_roleMap!=null)
1270 {
1271 String r=(String)_roleMap.get(role);
1272 if (r!=null)
1273 role=r;
1274 }
1275
1276 Principal principal = getUserPrincipal();
1277
1278 if (_userRealm!=null && principal!=null)
1279 return _userRealm.isUserInRole(principal, role);
1280
1281 return false;
1282 }
1283
1284
1285
1286
1287
1288 public void removeAttribute(String name)
1289 {
1290 Object old_value=_attributes==null?null:_attributes.getAttribute(name);
1291
1292 if (_attributes!=null)
1293 _attributes.removeAttribute(name);
1294
1295 if (old_value!=null)
1296 {
1297 if (_requestAttributeListeners!=null)
1298 {
1299 ServletRequestAttributeEvent event =
1300 new ServletRequestAttributeEvent(_context,this,name, old_value);
1301
1302 for(int i=0;i<LazyList.size(_requestAttributeListeners);i++)
1303 ((ServletRequestAttributeListener)LazyList.get(_requestAttributeListeners,i)).attributeRemoved(event);
1304 }
1305 }
1306 }
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319 public void setAttribute(String name, Object value)
1320 {
1321 Object old_value=_attributes==null?null:_attributes.getAttribute(name);
1322
1323 if ("org.mortbay.jetty.Request.queryEncoding".equals(name))
1324 setQueryEncoding(value==null?null:value.toString());
1325 else if("org.mortbay.jetty.ResponseBuffer".equals(name))
1326 {
1327 try
1328 {
1329 ByteBuffer byteBuffer=(ByteBuffer)value;
1330 synchronized (byteBuffer)
1331 {
1332 NIOBuffer buffer = byteBuffer.isDirect()
1333 ?(NIOBuffer)new DirectNIOBuffer(byteBuffer,true)
1334 :(NIOBuffer)new IndirectNIOBuffer(byteBuffer,true);
1335 ((HttpConnection.Output)getServletResponse().getOutputStream()).sendResponse(buffer);
1336 }
1337 }
1338 catch (IOException e)
1339 {
1340 throw new RuntimeException(e);
1341 }
1342 }
1343
1344
1345 if (_attributes==null)
1346 _attributes=new AttributesMap();
1347 _attributes.setAttribute(name, value);
1348
1349 if (_requestAttributeListeners!=null)
1350 {
1351 ServletRequestAttributeEvent event =
1352 new ServletRequestAttributeEvent(_context,this,name, old_value==null?value:old_value);
1353
1354 for(int i=0;i<LazyList.size(_requestAttributeListeners);i++)
1355 {
1356 ServletRequestAttributeListener l = (ServletRequestAttributeListener)LazyList.get(_requestAttributeListeners,i);
1357
1358 if (old_value==null)
1359 l.attributeAdded(event);
1360 else if (value==null)
1361 l.attributeRemoved(event);
1362 else
1363 l.attributeReplaced(event);
1364 }
1365 }
1366 }
1367
1368
1369
1370
1371
1372 public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException
1373 {
1374 if (_inputState!=__NONE)
1375 return;
1376
1377 _characterEncoding=encoding;
1378
1379
1380 if (!StringUtil.isUTF8(encoding))
1381 "".getBytes(encoding);
1382 }
1383
1384
1385
1386
1387
1388 public void setCharacterEncodingUnchecked(String encoding)
1389 {
1390 _characterEncoding=encoding;
1391 }
1392
1393
1394
1395
1396
1397
1398 private void extractParameters()
1399 {
1400 if (_baseParameters == null)
1401 _baseParameters = new MultiMap(16);
1402
1403 if (_paramsExtracted)
1404 {
1405 if (_parameters==null)
1406 _parameters=_baseParameters;
1407 return;
1408 }
1409
1410 _paramsExtracted = true;
1411
1412
1413 if (_uri!=null && _uri.hasQuery())
1414 {
1415 if (_queryEncoding==null)
1416 _uri.decodeQueryTo(_baseParameters);
1417 else
1418 {
1419 try
1420 {
1421 _uri.decodeQueryTo(_baseParameters,_queryEncoding);
1422
1423 }
1424 catch (UnsupportedEncodingException e)
1425 {
1426 if (Log.isDebugEnabled())
1427 Log.warn(e);
1428 else
1429 Log.warn(e.toString());
1430 }
1431 }
1432
1433 }
1434
1435
1436 String encoding = getCharacterEncoding();
1437 String content_type = getContentType();
1438 if (content_type != null && content_type.length() > 0)
1439 {
1440 content_type = HttpFields.valueParameters(content_type, null);
1441
1442 if (MimeTypes.FORM_ENCODED.equalsIgnoreCase(content_type) &&
1443 (HttpMethods.POST.equals(getMethod()) || HttpMethods.PUT.equals(getMethod())))
1444 {
1445 int content_length = getContentLength();
1446 if (content_length != 0)
1447 {
1448 try
1449 {
1450 int maxFormContentSize=-1;
1451
1452 if (_context!=null)
1453 maxFormContentSize=_context.getContextHandler().getMaxFormContentSize();
1454 else
1455 {
1456 Integer size = (Integer)_connection.getConnector().getServer().getAttribute("org.mortbay.jetty.Request.maxFormContentSize");
1457 if (size!=null)
1458 maxFormContentSize =size.intValue();
1459 }
1460
1461 if (content_length>maxFormContentSize && maxFormContentSize > 0)
1462 {
1463 throw new IllegalStateException("Form too large"+content_length+">"+maxFormContentSize);
1464 }
1465 InputStream in = getInputStream();
1466
1467
1468 UrlEncoded.decodeTo(in, _baseParameters, encoding,content_length<0?maxFormContentSize:-1);
1469 }
1470 catch (IOException e)
1471 {
1472 if (Log.isDebugEnabled())
1473 Log.warn(e);
1474 else
1475 Log.warn(e.toString());
1476 }
1477 }
1478 }
1479 }
1480
1481 if (_parameters==null)
1482 _parameters=_baseParameters;
1483 else if (_parameters!=_baseParameters)
1484 {
1485
1486 Iterator iter = _baseParameters.entrySet().iterator();
1487 while (iter.hasNext())
1488 {
1489 Map.Entry entry = (Map.Entry)iter.next();
1490 String name=(String)entry.getKey();
1491 Object values=entry.getValue();
1492 for (int i=0;i<LazyList.size(values);i++)
1493 _parameters.add(name, LazyList.get(values, i));
1494 }
1495 }
1496 }
1497
1498
1499
1500
1501
1502 public void setServerName(String host)
1503 {
1504 _serverName = host;
1505 }
1506
1507
1508
1509
1510
1511 public void setServerPort(int port)
1512 {
1513 _port = port;
1514 }
1515
1516
1517
1518
1519
1520 public void setRemoteAddr(String addr)
1521 {
1522 _remoteAddr = addr;
1523 }
1524
1525
1526
1527
1528
1529 public void setRemoteHost(String host)
1530 {
1531 _remoteHost = host;
1532 }
1533
1534
1535
1536
1537
1538 public HttpURI getUri()
1539 {
1540 return _uri;
1541 }
1542
1543
1544
1545
1546
1547 public void setUri(HttpURI uri)
1548 {
1549 _uri = uri;
1550 }
1551
1552
1553
1554
1555
1556 public HttpConnection getConnection()
1557 {
1558 return _connection;
1559 }
1560
1561
1562
1563
1564
1565 public int getInputState()
1566 {
1567 return _inputState;
1568 }
1569
1570
1571
1572
1573
1574 public void setAuthType(String authType)
1575 {
1576 _authType = authType;
1577 }
1578
1579
1580
1581
1582
1583 public void setCookies(Cookie[] cookies)
1584 {
1585 _cookies = cookies;
1586 }
1587
1588
1589
1590
1591
1592 public void setMethod(String method)
1593 {
1594 _method = method;
1595 }
1596
1597
1598
1599
1600
1601 public void setPathInfo(String pathInfo)
1602 {
1603 _pathInfo = pathInfo;
1604 }
1605
1606
1607
1608
1609
1610 public void setProtocol(String protocol)
1611 {
1612 _protocol = protocol;
1613 }
1614
1615
1616
1617
1618
1619 public void setRequestedSessionId(String requestedSessionId)
1620 {
1621 _requestedSessionId = requestedSessionId;
1622 }
1623
1624
1625
1626
1627
1628 public SessionManager getSessionManager()
1629 {
1630 return _sessionManager;
1631 }
1632
1633
1634
1635
1636
1637 public void setSessionManager(SessionManager sessionManager)
1638 {
1639 _sessionManager = sessionManager;
1640 }
1641
1642
1643
1644
1645
1646 public void setRequestedSessionIdFromCookie(boolean requestedSessionIdCookie)
1647 {
1648 _requestedSessionIdFromCookie = requestedSessionIdCookie;
1649 }
1650
1651
1652
1653
1654
1655 public void setSession(HttpSession session)
1656 {
1657 _session = session;
1658 }
1659
1660
1661
1662
1663
1664 public void setScheme(String scheme)
1665 {
1666 _scheme = scheme;
1667 }
1668
1669
1670
1671
1672
1673 public void setQueryString(String queryString)
1674 {
1675 _queryString = queryString;
1676 }
1677
1678
1679
1680
1681 public void setRequestURI(String requestURI)
1682 {
1683 _requestURI = requestURI;
1684 }
1685
1686
1687
1688
1689
1690 public void setContextPath(String contextPath)
1691 {
1692 _contextPath = contextPath;
1693 }
1694
1695
1696
1697
1698
1699 public void setServletPath(String servletPath)
1700 {
1701 _servletPath = servletPath;
1702 }
1703
1704
1705
1706
1707
1708 public void setServletName(String name)
1709 {
1710 _servletName = name;
1711 }
1712
1713
1714
1715
1716
1717 public void setUserPrincipal(Principal userPrincipal)
1718 {
1719 _userPrincipal = userPrincipal;
1720 }
1721
1722
1723
1724
1725
1726 public void setContext(SContext context)
1727 {
1728 _context=context;
1729 }
1730
1731
1732
1733
1734
1735
1736 public SContext getContext()
1737 {
1738 return _context;
1739 }
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753 public StringBuffer getRootURL()
1754 {
1755 StringBuffer url = new StringBuffer(48);
1756 synchronized (url)
1757 {
1758 String scheme = getScheme();
1759 int port = getServerPort();
1760
1761 url.append(scheme);
1762 url.append("://");
1763 url.append(getServerName());
1764
1765 if (port > 0 && ((scheme.equalsIgnoreCase("http") && port != 80) || (scheme.equalsIgnoreCase("https") && port != 443)))
1766 {
1767 url.append(':');
1768 url.append(port);
1769 }
1770 return url;
1771 }
1772 }
1773
1774
1775
1776
1777 public Attributes getAttributes()
1778 {
1779 if (_attributes==null)
1780 _attributes=new AttributesMap();
1781 return _attributes;
1782 }
1783
1784
1785
1786
1787 public void setAttributes(Attributes attributes)
1788 {
1789 _attributes=attributes;
1790 }
1791
1792
1793 public Continuation getContinuation()
1794 {
1795 return _continuation;
1796 }
1797
1798
1799 public Continuation getContinuation(boolean create)
1800 {
1801 if (_continuation==null && create)
1802 _continuation=getConnection().getConnector().newContinuation();
1803 return _continuation;
1804 }
1805
1806
1807 void setContinuation(Continuation cont)
1808 {
1809 _continuation=cont;
1810 }
1811
1812
1813
1814
1815
1816 public MultiMap getParameters()
1817 {
1818 return _parameters;
1819 }
1820
1821
1822
1823
1824
1825 public void setParameters(MultiMap parameters)
1826 {
1827 _parameters= (parameters==null)?_baseParameters:parameters;
1828 if (_paramsExtracted && _parameters==null)
1829 throw new IllegalStateException();
1830 }
1831
1832
1833 public String toString()
1834 {
1835 return getMethod()+" "+_uri+" "+getProtocol()+"\n"+
1836 _connection.getRequestFields().toString();
1837 }
1838
1839
1840 public static Request getRequest(HttpServletRequest request)
1841 {
1842 if (request instanceof Request)
1843 return (Request) request;
1844
1845 while (request instanceof ServletRequestWrapper)
1846 request = (HttpServletRequest)((ServletRequestWrapper)request).getRequest();
1847
1848 if (request instanceof Request)
1849 return (Request) request;
1850
1851 return HttpConnection.getCurrentConnection().getRequest();
1852 }
1853
1854
1855
1856 public synchronized void addEventListener(EventListener listener)
1857 {
1858 if (listener instanceof ServletRequestAttributeListener)
1859 _requestAttributeListeners= LazyList.add(_requestAttributeListeners, listener);
1860 }
1861
1862
1863 public synchronized void removeEventListener(EventListener listener)
1864 {
1865 _requestAttributeListeners= LazyList.remove(_requestAttributeListeners, listener);
1866 }
1867
1868
1869 public void saveNewSession(Object key,HttpSession session)
1870 {
1871 if (_savedNewSessions==null)
1872 _savedNewSessions=new HashMap();
1873 _savedNewSessions.put(key,session);
1874 }
1875
1876 public HttpSession recoverNewSession(Object key)
1877 {
1878 if (_savedNewSessions==null)
1879 return null;
1880 return (HttpSession) _savedNewSessions.get(key);
1881 }
1882
1883
1884
1885
1886
1887 public UserRealm getUserRealm()
1888 {
1889 return _userRealm;
1890 }
1891
1892
1893
1894
1895
1896 public void setUserRealm(UserRealm userRealm)
1897 {
1898 _userRealm = userRealm;
1899 }
1900
1901
1902 public String getQueryEncoding()
1903 {
1904 return _queryEncoding;
1905 }
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917 public void setQueryEncoding(String queryEncoding)
1918 {
1919 _queryEncoding=queryEncoding;
1920 _queryString=null;
1921 }
1922
1923
1924 public void setRoleMap(Map map)
1925 {
1926 _roleMap=map;
1927 }
1928
1929
1930 public Map getRoleMap()
1931 {
1932 return _roleMap;
1933 }
1934
1935
1936 public ServletContext getServletContext()
1937 {
1938 return _context;
1939 }
1940
1941
1942 public ServletResponse getServletResponse()
1943 {
1944 return _connection.getResponse();
1945 }
1946 }
1947