1 45 package org.openejb.server.httpd; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.DataInput ; 49 import java.io.DataInputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.net.URI ; 53 import java.net.URISyntaxException ; 54 import java.net.URLDecoder ; 55 import java.util.HashMap ; 56 import java.util.Map ; 57 import java.util.StringTokenizer ; 58 59 63 public class HttpRequestImpl implements HttpRequest { 64 public static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded"; 65 public static final String TEXT_XML = "text/xml"; 66 public static final String MULITPART_FORM_DATA = "multipart/form-data"; 67 public static final String FILENAME = "filename"; 68 public static final String NAME = "name"; 69 70 73 private String line; 74 77 private int method; 78 81 private URI uri; 82 85 private HashMap headers; 86 89 private HashMap formParams = new HashMap (); 90 93 private HashMap queryParams = new HashMap (); 94 private HashMap parameters = new HashMap (); 95 96 protected static final String EJBSESSIONID = "EJBSESSIONID"; 97 98 private HashMap cookies; 99 100 101 104 private byte[] body; 105 private InputStream in; 106 private int length; 107 private String contentType; 108 109 private final URI socketURI; 110 111 private HashMap attributes = new HashMap (); 112 113 public HttpRequestImpl(URI socketURI) { 114 this.socketURI = socketURI; 115 } 116 117 123 public String getHeader(String name) { 124 return (String ) headers.get(name); 125 } 126 127 133 public String getFormParameter(String name) { 134 return (String ) formParams.get(name); 135 } 136 137 public Map getFormParameters() { 138 return (Map )formParams.clone(); 139 } 140 141 public Map getQueryParameters() { 142 return (Map )queryParams.clone(); 143 } 144 145 151 public String getQueryParameter(String name) { 152 return (String ) queryParams.get(name); 153 } 154 155 170 public int getMethod() { 171 return method; 172 } 173 174 179 public URI getURI() { 180 return uri; 181 } 182 183 public int getContentLength() { 184 return length; 185 } 186 187 public String getContentType() { 188 return contentType; 189 } 190 191 public InputStream getInputStream() throws IOException { 192 return this.in; 193 } 194 195 196 197 198 204 protected void readMessage(InputStream input) throws IOException { 205 DataInput in = new DataInputStream (input); 206 207 readRequestLine(in); 208 readHeaders(in); 209 readBody(in); 210 211 parameters = new HashMap (); 212 parameters.putAll(this.getFormParameters()); 213 parameters.putAll(this.getQueryParameters()); 214 } 215 216 222 private void readRequestLine(DataInput in) throws IOException { 223 try { 224 line = in.readLine(); 225 } catch (Exception e) { 227 throw new IOException ("Could not read the HTTP Request Line :" 228 + e.getClass().getName() 229 + " : " 230 + e.getMessage()); 231 } 232 233 StringTokenizer lineParts = new StringTokenizer (line, " "); 234 235 parseMethod(lineParts); 236 237 parseURI(lineParts); 238 } 239 240 246 private void parseMethod(StringTokenizer lineParts) throws IOException { 247 String token = null; 248 try { 249 token = lineParts.nextToken(); 250 } catch (Exception e) { 251 throw new IOException ("Could not parse the HTTP Request Method :" 252 + e.getClass().getName() 253 + " : " 254 + e.getMessage()); 255 } 256 257 if (token.equalsIgnoreCase("GET")) { 258 method = GET; 259 } else if (token.equalsIgnoreCase("POST")) { 260 method = POST; 261 } else { 262 method = UNSUPPORTED; 263 throw new IOException ("Unsupported HTTP Request Method :" + token); 264 } 265 } 266 267 273 private void parseURI(StringTokenizer lineParts) throws IOException { 274 String token = null; 275 try { 276 token = lineParts.nextToken(); 277 } catch (Exception e) { 278 throw new IOException ("Could not parse the HTTP Request Method :" 279 + e.getClass().getName() 280 + " : " 281 + e.getMessage()); 282 } 283 284 try { 285 uri = new URI (socketURI.toString()+token); 286 } catch (URISyntaxException e) { 287 throw new IOException ("Malformed URI :" + token + " Exception: " + e.getMessage()); 288 } 289 290 parseQueryParams(uri.getQuery()); 291 } 292 293 298 private void parseQueryParams(String query) { 299 if (query == null) 300 return; 301 StringTokenizer parameters = new StringTokenizer (query, "&"); 302 303 while (parameters.hasMoreTokens()) { 304 StringTokenizer param = new StringTokenizer (parameters.nextToken(), "="); 305 306 307 if (!param.hasMoreTokens()) 308 continue; 309 String name = URLDecoder.decode(param.nextToken()); 310 if (name == null) 311 continue; 312 313 String value; 314 315 if (!param.hasMoreTokens()){ 316 value = ""; 317 } else { 318 value = URLDecoder.decode(param.nextToken()); 319 } 320 321 queryParams.put(name, value); 323 } 324 } 325 326 332 private void readHeaders(DataInput in) throws IOException { 333 headers = new HashMap (); 335 while (true) { 336 String hf = null; 338 339 try { 340 hf = in.readLine(); 341 } catch (Exception e) { 343 throw new IOException ("Could not read the HTTP Request Header Field :" 344 + e.getClass().getName() 345 + " : " 346 + e.getMessage()); 347 } 348 349 if (hf == null || hf.equals("")) { 350 break; 351 } 352 353 354 int colonIndex = hf.indexOf((int) ':'); 355 String name = hf.substring(0, colonIndex); 356 if (name == null) 357 break; 358 359 360 String value = hf.substring(colonIndex + 1, hf.length()); 361 if (value == null) 362 break; 363 value = value.trim(); 364 headers.put(name, value); 365 } 366 367 String host = (String ) headers.get("Host"); 369 if( host!=null ) { 370 String hostName = uri.getHost(); 371 int port = uri.getPort(); 372 int idx = host.indexOf(":"); 373 if( idx >= 0 ) { 374 hostName = host.substring(0, idx); 375 try { 376 port = Integer.parseInt(host.substring(idx+1)); 377 } 378 catch (NumberFormatException ignore) { 379 } 380 } else { 381 hostName = host; 382 } 383 384 try { 385 uri = new URI (uri.getScheme(), 386 uri.getUserInfo(), hostName, port, 387 uri.getPath(), uri.getQuery(), 388 uri.getFragment()); 389 } catch (URISyntaxException ignore) { 390 } 391 } 392 393 } 402 403 409 private void readBody(DataInput in) throws IOException { 410 length = parseContentLength(); 414 415 contentType = getHeader(HttpRequest.HEADER_CONTENT_TYPE); 416 417 if (method == POST && FORM_URL_ENCODED.equals(contentType)) { 418 String rawParams = null; 419 420 try { 421 byte[] body = new byte[length]; 422 in.readFully(body); 423 rawParams = new String (body); 424 } catch (Exception e) { 425 throw (IOException )new IOException ("Could not read the HTTP Request Body: " + e.getMessage()).initCause(e); 426 } 427 428 StringTokenizer parameters = new StringTokenizer (rawParams, "&"); 429 String name = null; 430 String value = null; 431 432 while (parameters.hasMoreTokens()) { 433 StringTokenizer param = new StringTokenizer (parameters.nextToken(), "="); 434 435 436 name = URLDecoder.decode(param.nextToken()); 437 if (name == null) 438 break; 439 440 441 if (param.hasMoreTokens()) { 442 value = URLDecoder.decode(param.nextToken()); 443 } else { 444 value = ""; } 446 447 if (value == null) 448 value = ""; 449 450 formParams.put(name, value); 451 } 453 } else if (method == POST){ 454 byte[] body = new byte[length]; 456 in.readFully(body); 457 this.in = new ByteArrayInputStream (body); 458 } else { 459 this.in = new ByteArrayInputStream (new byte[0]); 460 } 461 462 } 463 464 private int parseContentLength() { 465 String len = getHeader(HttpRequest.HEADER_CONTENT_LENGTH); 467 469 int length = -1; 470 if (len != null) { 471 try { 472 length = Integer.parseInt(len); 473 } catch (Exception e) { 474 } 476 } 477 return length; 478 } 479 480 protected HashMap getCookies() { 481 if (cookies != null) return cookies; 482 483 cookies = new HashMap (); 484 485 String cookieHeader = getHeader(HEADER_COOKIE); 486 if (cookieHeader == null) return cookies; 487 488 StringTokenizer tokens = new StringTokenizer (cookieHeader, ";"); 489 while (tokens.hasMoreTokens()) { 490 StringTokenizer token = new StringTokenizer (tokens.nextToken(), "="); 491 String name = token.nextToken(); 492 String value = token.nextToken(); 493 cookies.put(name, value); 494 } 495 return cookies; 496 } 497 498 protected String getCookie(String name) { 499 return (String ) getCookies().get(name); 500 } 501 502 public HttpSession getSession(boolean create) { 503 return null; 504 } 505 506 public HttpSession getSession() { 507 return getSession(true); 508 } 509 510 public Object getAttribute(String name) { 511 return attributes.get(name); 512 } 513 514 public void setAttribute(String name, Object value){ 515 attributes.put(name, value); 516 } 517 518 public String getParameter(String name) { 519 return (String )parameters.get(name); 520 } 521 522 public Map getParameters() { 523 return (Map )parameters.clone(); 524 } 525 } | Popular Tags |