Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

HTTPSocket.java

Go to the documentation of this file.
00001 /*
00002  * HTTPSocket.java
00003  *
00004  * Created on den 28 oktober 2004, 15:18
00005  */
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008  
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018  
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022  */
00023 
00024 package net.alhem.jsockets;
00025 
00026 import java.lang.Character;
00027 
00032 public abstract class HTTPSocket extends TcpSocket
00033 {
00034     
00036     public HTTPSocket(SocketHandler h)
00037     {
00038         super(h);
00039         SetLineProtocol();
00040     }
00041 
00042     public void OnRead()
00043     {
00044         super.OnRead();
00045         if (!m_header)
00046         {
00047             if (m_ibuf.limit() > 0)
00048             {
00049                 byte[] buf = new byte[m_ibuf.limit()]; // huvva
00050                 m_ibuf.get(buf);
00051                 OnData(buf, buf.length);
00052                 buf = null;
00053             }
00054         }
00055     } // OnRead
00056 
00057     public void ReadLine()
00058     {
00059         if (m_ibuf.limit() > 0)
00060         {
00061             byte[] buf = new byte[m_ibuf.limit()]; // huvva
00062             m_ibuf.get(buf);
00063             for (int i = 0; i < buf.length; i++)
00064             {
00065                 if (!m_header)
00066                 {
00067                     int sz = buf.length - i;
00068                     byte[] buf2 = new byte[sz];
00069                     for (int j = 0; j < sz; j++)
00070                     {
00071                         buf2[j] = buf[j + i];
00072                     }
00073                     OnData(buf2, sz);
00074                     buf2 = null;
00075                     break;
00076                 }
00077                 switch (buf[i])
00078                 {
00079                     case 13:
00080                         break;
00081                     case 10:
00082                         OnLine(m_line.toString());
00083                         m_line.delete(0, m_line.length());
00084                         break;
00085                     default:
00086                     {
00087                         String aChar = new Character((char)buf[i]).toString();
00088                         m_line.append(aChar);
00089                     }
00090                 }
00091             }
00092             //
00093             buf = null;
00094         }
00095     } // ReadLine
00096 
00097     public void OnLine(String line)
00098     {
00099         if (m_first)
00100         {
00101             Parse pa = new Parse(line);
00102             String str = pa.getword();
00103             if (str.length() >= 4 && str.substring(0,4).equals("HTTP")) // response
00104             {
00105                 m_http_version = str;
00106                 m_status = pa.getword();
00107                 m_status_text = pa.getrest();
00108                 m_response = true;
00109             }
00110             else // request
00111             {
00112                 m_method = str;
00113                 m_url = pa.getword();
00115                 m_http_version = pa.getword();
00116                 m_request = true;
00117             }
00118             m_first = false;
00119             OnFirst();
00120             return;
00121         }
00122         if (line.length() == 0)
00123         {
00124             m_header = false;
00125             OnHeaderComplete();
00126             return;
00127         }
00128         Parse pa = new Parse(line, ":");
00129         String key = pa.nextToken();
00130         String value = pa.getrest();
00131         OnHeader(key, value);
00132     } // OnLine
00133 
00134     public abstract void OnData(byte[] buf,int l);
00135 
00136     public abstract void OnFirst();
00137 
00138     public abstract void OnHeader(String key,String value);
00139     
00140     public abstract void OnHeaderComplete();
00141 
00142     public boolean IsRequest()
00143     {
00144         return m_request;
00145     }
00146     public boolean IsResponse()
00147     {
00148         return m_response;
00149     }
00150     public String GetHttpVersion()
00151     {
00152         return m_http_version;
00153     }
00154     public String GetStatus()
00155     {
00156         return m_status;
00157     }
00158     public String GetStatusText()
00159     {
00160         return m_status_text;
00161     }
00162     public String GetMethod()
00163     {
00164         return m_method;
00165     }
00166     public String GetUrl()
00167     {
00168         return m_url;
00169     }
00170     public String GetUri()
00171     {
00172         return m_uri;
00173     }
00174     public String GetQueryString()
00175     {
00176         return m_query_string;
00177     }
00178 
00179     //
00180     private boolean m_first = true;
00181     private boolean m_header = true;
00182     private boolean m_request = false;
00183     private boolean m_response = false;
00184     // response
00185     private String m_http_version;
00186     private String m_status;
00187     private String m_status_text;
00188     // request
00189     private String m_method;
00190     private String m_url;
00191     private String m_uri;
00192     private String m_query_string;
00193 
00194 }

Generated on Fri Oct 29 14:11:17 2004 for Java Sockets by  doxygen 1.3.9.1