//package jp.tf_web.httpserversample.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.CharArrayBuffer;
import android.util.Log;
import android.webkit.MimeTypeMap;
class HttpUtil {
public final static String HTTP_PROTOCOL = "HTTP/1.1";
public final static String HEADER_NAME_REQUESTLINE = "RequestLine";
public final static String HEADER_NAME_CONTENT_LENGTH = "Content-Length";
private HttpUtil(){}
//??????????????
public static HashMap parseRequestHeader(String headers_str) throws IOException{
//????????
StringReader sr = new StringReader(headers_str);
BufferedReader bur = new BufferedReader(sr);
HashMap headerList = new HashMap();
String line;
int line_index = 0;
while((line = bur.readLine()) != null){
if(line.length() == 0) break;
//????????
if(line_index == 0){
//????????
BasicLineParser parser = new BasicLineParser();
RequestLine request_line = BasicLineParser.parseRequestLine(line, parser);
headerList.put(HttpUtil.HEADER_NAME_REQUESTLINE,request_line);
}
else{
//????
CharArrayBuffer cab = new CharArrayBuffer(line.length());
cab.append(line);
Header h = new BufferedHeader(cab);
headerList.put(h.getName(),h);
}
line_index++;
}
bur.close();
sr.close();
return headerList;
}
//???????????
public static StatusLine createStatusLine(int status,String reason){
BasicLineParser parser = new BasicLineParser();
return BasicLineParser.parseStatusLine(HTTP_PROTOCOL+" "+status+" "+reason,parser);
}
//HTTP????????
public static HttpResponse createHttpResponse(int status,String reason,HttpEntity entity){
StatusLine header_sl = HttpUtil.createStatusLine(status,reason);
HttpResponse hr = new BasicHttpResponse(header_sl);
hr.setEntity(entity);
return hr;
}
public static HttpEntity createHttpEntity(String contentType,byte[] entity_buf){
ByteArrayEntity he = new ByteArrayEntity(entity_buf);
he.setContentType(contentType);
return he;
}
public static String getMimeTypeFromUri(String uri){
//?????MimeType???
String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(uri);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
//????? "application/octet-stream"
//ToDo mime-type????????????mime-type???????????????
mimeType = (mimeType != null)?mimeType:"application/octet-stream";
Log.d(HttpUtil.class.getName(),"ext:"+ext+" mimeType:"+mimeType);
return mimeType;
}
}