HTTPRequestHeader.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:7k
- /* $Id: HTTPRequestHeader.java,v 1.3 2000/04/18 13:13:24 wastl Exp $ */
- package net.wastl.webmail.server.http;
- import java.io.*;
- import java.util.*;
- import javax.activation.DataSource;
- import javax.mail.*;
- import javax.mail.internet.*;
- import gnu.regexp.*;
- import net.wastl.webmail.server.*;
- import net.wastl.webmail.misc.*;
- import net.wastl.webmail.server.TimeoutException;
- import net.wastl.webmail.debug.ErrorHandler;
- /**
- * HTTPHeader.java
- *
- *
- * Created: Tue Feb 2 15:25:48 1999
- *
- * @author Sebastian Schaffert
- * @version
- */
- public class HTTPRequestHeader {
- private Hashtable content;
- private Hashtable headers;
- public HTTPRequestHeader() {
- headers=new Hashtable(10,.9f);
- content=new Hashtable(5,.9f);
- }
- public String getMethod() {
- return getHeader("Method");
- }
- public String getPath() {
- return getHeader("Path");
- }
- public String getVersion() {
- return getHeader("Version");
- }
- public void setPath(String s) {
- setHeader("PATH",Urlify.unUrlify(s));
- }
- public void setMethod(String s) {
- setHeader("METHOD",s);
- }
- public void setVersion(String s) {
- setHeader("VERSION",s);
- }
- public void setEncodedContent(String url_encoded, String contenttype) {
- if(contenttype.toLowerCase().startsWith("application/x-www-form-urlencoded")) {
- StringTokenizer t=new StringTokenizer(url_encoded,"&");
- while(t.hasMoreTokens()) {
- try {
- String str=t.nextToken();
- StringTokenizer tok=new StringTokenizer(str,"=rn");
- String key="";
- String value="";
- try {
- key=Urlify.unUrlify(tok.nextToken());
- value=Urlify.unUrlify(tok.nextToken());
- } catch(Exception e) {
- } finally {
- content.put(key.toUpperCase(),value);
- }
- } catch(NoSuchElementException e) {
- e.printStackTrace();
- System.err.println("While reading post");
- }
- }
- } else if(contenttype.toLowerCase().startsWith("multipart/form-data")) {
- String s="Content-type: "+headers.get("CONTENT-TYPE")+"nContent-length: "+headers.get("CONTENT-LENGTH")+"n"
- + url_encoded;
- try {
- HTTPDataSource ds=new HTTPDataSource(s,((String)headers.get("CONTENT-TYPE")));
- MimeMultipart m=new MimeMultipart(ds);
- //System.err.println("Parts: "+m.getCount());
- for(int i=0;i<m.getCount();i++) {
- MimeBodyPart p=(MimeBodyPart)m.getBodyPart(i);
-
- Enumeration enum=p.getAllHeaders();
- while(enum.hasMoreElements()) {
- Header h=(Header)enum.nextElement();
- //System.err.println(h.getName()+" "+h.getValue());
- }
- p.setHeader("Content-Transfer-Encoding","binary");
- p.setHeader("Content-Type","application/octet-stream");
- //System.err.println(p.getFileName());
-
- String disp=p.getHeader("CONTENT-DISPOSITION",";");
- RE name_re=new RE("name\="([^"]+)"",RE.REG_MULTILINE);
- REMatch name_m=name_re.getMatch(disp);
- String name=name_m.substituteInto("$1");
-
- ByteStore bs=parseMIME(p);
- bs.setName(p.getFileName());
- bs.setContentType(WebMailServer.getStorage().getMimeType(p.getFileName()));
- content.put(name.toUpperCase(),bs);
-
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
-
- } else {
- System.err.println("Unsupported contenttype! "+contenttype);
- }
- }
- public void setHeader(String key, String value) {
- if(headers == null) {
- headers=new Hashtable();
- }
- //System.out.println("DEBUG: Setting "+key.trim().toUpperCase()+" to "+value);
- headers.put(key.toUpperCase(), value);
- }
- public String getHeader(String t) {
- return (String)headers.get(t.toUpperCase());
- }
- public Hashtable getContent() {
- return content;
- }
- public Object getObjContent(String key) {
- if(content!=null) {
- return content.get(key.toUpperCase());
- } else {
- return null;
- }
- }
- public String getContent(String key) {
- if(content!=null) {
- Object o=content.get(key.toUpperCase());
- if(o == null) {
- return null;
- } else if(o instanceof String) {
- return (String)o;
- } else if(o instanceof ByteStore) {
- return new String(((ByteStore)o).getBytes());
- } else {
- return "";
- }
- //return (String)content.get(key.toUpperCase());
- } else {
- return "";
- }
- }
- public boolean isContentSet(String key) {
- Object s=content.get(key.toUpperCase());
- return s != null && !(s instanceof String && ((String)s).equals(""));
- }
- public void setContent(String key, String value) {
- content.put(key.toUpperCase(),value);
- }
- public Enumeration getHeaderKeys() {
- return headers.keys();
- }
- public Enumeration getContentKeys() {
- return content.keys();
- }
- public String toString() {
- String s="Method: "+headers.get("METHOD")+", Path="+headers.get("PATH")+", HTTP-version: "+headers.get("VERSION")+"n";
- s+="HTTP Headers:n";
- Enumeration e=headers.keys();
- while(e.hasMoreElements()) {
- String h=(String)e.nextElement();
- s+="Header name: "+h+", value: "+headers.get(h)+"n";
- }
- if(content != null) {
- s+="Form Content:n";
- e=content.keys();
- while(e.hasMoreElements()) {
- String h=(String)e.nextElement();
- s+="Header name: "+h+", value: "+content.get(h)+"n";
- }
- }
- return s;
- }
-
-
- protected static ByteStore parseMIME(MimeBodyPart p) {
- try {
- InputStream in=null;
- String type="";
- int size=Integer.parseInt(WebMailServer.getStorage().getConfig("max attach size"));
- if(p instanceof MimeBodyPart) {
- MimeBodyPart mpb=(MimeBodyPart)p;
- InputStream is=mpb.getInputStream();
-
- /* Workaround for Java or Javamail Bug */
- in=new BufferedInputStream(is);
- ByteStore ba=ByteStore.getBinaryFromIS(in,size);
- in=new ByteArrayInputStream(ba.getBytes());
- /* End of workaround */
- size=in.available();
-
- } else {
- //System.err.println("*** No MIME Body part!! ***");
- in=p.getInputStream();
- }
-
- ByteStore data=ByteStore.getBinaryFromIS(in,size);
- //data.setContentType(p.getContentType());
- data.setContentEncoding(p.getHeader("Content-Transfer-Encoding")[0]);
- return data;
- } catch(Exception e) {
- return null;
- }
- }
-
- protected class HTTPDataSource implements DataSource {
- InputStream is;
- String contenttype;
-
- HTTPDataSource(String in, String cont) {
- contenttype=cont;
- is=new ByteArrayInputStream(in.getBytes());
- }
- public InputStream getInputStream() throws IOException {
- if(is==null) {
- throw new IOException("No InputStream");
- }
- return is;
- }
- public OutputStream getOutputStream() throws IOException {
- throw new IOException();
- }
- public String getContentType() {
- return contenttype;
- }
- public String getName() {
- return "NoName";
- }
- }
- } // HTTPHeader