?? httpdecoder.java
字號:
package freech.core;
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class HttpDecoder {
private Socket s = null;
private String method =null;
private String action = null;
private Hashtable params = null;
private Hashtable header = null;
private String cookie = null;
public HttpDecoder(Socket s) {
this.s = s;
this.params = new Hashtable();
this.header = new Hashtable();
try {
parse();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void parse() throws IOException, InterruptedException {
InputStream is = s.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
if (in == null) {
//
}
StringTokenizer st = new StringTokenizer(in.readLine() );
if (!st.hasMoreTokens()) {
System.out
.println("bad request:Syntax error. Usage: GET /example/file.html");
}
method = st.nextToken();// get,post
if (!st.hasMoreTokens()) {
System.out
.println("BAD REQUEST: Missing URI. Usage: GET /example/file.html");
}
//讀取uri
String uri = decodePercent(st.nextToken());
int pos = uri.indexOf("?");//參數(shù)可能接在?之后
if (pos > -1) {
action = decodePercent(uri.substring(0, pos));
DecodeParams(uri.substring( pos+1));
}else{
action =decodePercent(uri);
}
//讀頭部
String line = in.readLine() ;
while(line.trim() .length() >0){
int p=line.indexOf( ":");
String key = line.substring( 0,p).trim().toLowerCase();
String value = line.substring( p+1).trim();
if(key.equals( "cookie")){
String[] cookiepair = value.split( ";");
for(int i = 0;i<cookiepair.length ;i++){
String[] cp = cookiepair[i].trim() .split( "=");
if(cp.length <2){
continue;
}
if(!cookiepair[i].trim() .startsWith( "FreeChSession")){
continue;
}
value = cp[1].trim() ;//cookie的值
}
}
header.put( key ,value );
line = in.readLine() ;
}
//到這里,是一個(gè)空行
//如果方法是POST, 可能參數(shù)在數(shù)據(jù)段中,讀取它:
if(method.equalsIgnoreCase( "POST")){
int size = Integer.MAX_VALUE ;
String contentlength = (String)header.get( "content-length");
if(contentlength!=null){
size = Integer.parseInt( contentlength);
}
String postline="";//所有參數(shù)
char[] buf = new char[512];
int readlen=in.read(buf) ;
while(readlen >=0&&size>0&&!postline.endsWith( "\r\n")){
size-=readlen;
postline += String.valueOf(buf,0,readlen);
if(size>0){
readlen =in.read( buf);
}
}
postline = postline.trim() ;
DecodeParams(postline);
}
//in.close() ;
printData();
}
private String decodePercent( String str ) throws InterruptedException
{
try
{
StringBuffer sb = new StringBuffer();
for( int i=0; i<str.length(); i++ )
{
char c = str.charAt( i );
switch ( c )
{
case '+':
sb.append( ' ' );
break;
case '%':
sb.append((char)Integer.parseInt( str.substring(i+1,i+3), 16 ));
i += 2;
break;
default:
sb.append( c );
break;
}
}
return new String( sb.toString().getBytes());
}
catch( Exception e )
{
System.out.println("BAD REQUEST: Bad percent-encoding." );
return null;
}
}
private void DecodeParams(String param) throws InterruptedException, UnsupportedEncodingException{
if (param == null){
return;
}
StringTokenizer st = new StringTokenizer(param,"&");
while(st.hasMoreTokens() ){
String e = st.nextToken() ;
//StringTokenizer st1 = new StringTokenizer(e,"=");
//if(st1.hasMoreTokens() ){
//String param_name =st1.nextToken().trim();
//String param_value = st1.nextToken().trim();//此處出錯(cuò)
int sep = e.indexOf( '=');
if(sep>=0){
String param_name=decodePercent(e.substring( 0,sep).trim());
//param_name =new String(param_name.getBytes( "BIG5"),"gb2312");
String param_value = decodePercent(e.substring( sep+1).trim());
param_value =new String(param_value.getBytes( "UTF8"));
params.put( param_name,param_value);
}
}
}
private void printData(){
System.out.println( method + " '" + action + "' " );
Enumeration e = header.keys() ;
while(e.hasMoreElements() ){
String name = (String)e.nextElement() ;
String value = (String)header.get(name);
System.out.println( " HDR: '" + name + "' = '" + value + "'" );
}
e = params.keys() ;
while(e.hasMoreElements() ){
String name = (String)e.nextElement() ;
String value = (String)params.get(name);
System.out.println( " PRM: '" + name + "' = '" + value + "'" );
}
}
public String getAction() {
if(action.length() !=1){
int pos = action.indexOf('/');
if(pos == 0){
action = action.substring( 1);//去除"/"
}
}
return action;
}
public Hashtable getParam() {
return params;
}
public String getCookie() {
return (String)header.get( "cookie");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -