Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SpyMomiji's HTTPHeaderScanner
- * 版本 1.0.2
- * 日期 2014/03/04
- * https://plus.google.com/u/0/107057557978482828685/posts
- */
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.TreeMap;
- public class HTTPHeaderScanner extends Thread{
- public ArrayList<String> header = new ArrayList<String>(); //Header 的內容
- public ArrayList<String> headerKeys = new ArrayList<String>(); //headerValues 所有的鍵值
- public TreeMap<String, String> headerValues = new TreeMap<String, String>(); //以 Header 的項目為鍵值,查詢項目內容
- public byte[] data; //接在 Header 後的資料,以陣列儲存以方便程式解讀
- public String method = null; //HTTP 方法
- public String path = null; //HTTP 要求的路徑 (方法為 HTTP 時此屬性為 null)
- public String stats = null; //HTTP 要求的狀態 ex:"200 OK"
- public boolean headerReady = false; //表示準備完成(Header接收完成)
- public boolean dataReady = false; //表示準備完成(資料接收完成)
- public boolean closeAfterRcev = false; //接收結束後切斷串流
- public boolean alert = true; //發生例外狀況時印出訊息
- public Exception exception = null; //錯誤產生時,例外類別會被放到這
- //使用方法
- //先建立或取得 Socket 物件
- //建立 HTTPHeaderScanner 物件,參數使用 (Socket)object.getOutputStream()
- //接著 start()
- //getData() 直接讀取已經載入的資料
- //waitfor() 可取代直接使用 join()
- //close() 同關閉串流
- public HTTPHeaderScanner(InputStream inputStream) {
- is = inputStream;
- }
- public HTTPHeaderScanner(InputStream inputStream,boolean closeAfterRcev) {
- is = inputStream;
- this.closeAfterRcev = closeAfterRcev;
- }
- ByteArrayOutputStream ba = null;
- InputStream is;
- public void run() {
- try{
- try{
- int ch;
- int bc = -1;
- while(true){
- //建立新緩衝區
- ba = new ByteArrayOutputStream();
- //若上一圈有不小心掃到下一行的字節,將在這邊先行加入
- if(bc!=-1){
- ba.write(bc);
- bc = -1;
- }
- while(true){
- ch = is.read();
- if(ch == 13 || ch == 10){
- ch = is.read();
- if(ch == 13 || ch == 10){
- break;
- }
- bc = ch;
- break;
- }
- if(ch<0)throw new Exception("Data truncated");
- //寫入緩衝
- ba.write(ch);
- }
- if(ba.size()==0){
- break;
- }
- String t = new String(ba.toByteArray(),"UTF-8");
- // System.out.println(t);
- header.add(t);
- try{
- String[] ts = t.split("\\s*:\\s*");
- headerValues.put(ts[0], ts[1]);
- headerKeys.add(ts[0]);
- }catch(Exception e){
- // System.out.println("\t" + t);
- String[] ts = t.split("\\s",3);
- //求出狀態、路徑和訊息
- if(ts[0].toLowerCase().matches("[get|post]{1,}")){
- // System.out.println(ts[0] + "\t" + ts[1] + "\t" + ts[2] + "\t");
- method = ts[0];
- path = ts[1];
- stats = ts[2];
- }else if(ts[0].toLowerCase().matches("http.*")){
- method = ts[0];
- stats = ts[1] + " " + ts[2];
- }
- }
- }
- }catch(IOException e){
- if(alert)e.printStackTrace();
- if(ba.size()!=0)
- header.add(new String(ba.toByteArray(),"UTF-8"));
- }
- headerReady = true;
- // System.out.println(header.size());
- // System.out.println(header);
- // for(String i : header){
- // System.out.println(i);
- // }
- // System.out.println(headerReady);
- // System.out.println(path);
- ba = new ByteArrayOutputStream();
- try{
- int length = Integer.parseInt(headerValues.get("Content-Length"));
- if(length==0)throw new Exception("EmptyDataException");
- for(int i=0;i<length;i++){
- ba.write(is.read());
- }
- data = ba.toByteArray();
- }catch(NumberFormatException e){
- TimeoutTimer tt = new TimeoutTimer(this,1000);
- tt.start();
- int bc = 0;
- do{
- bc = is.read();
- ba.write(bc);
- tt.reset();
- }while(bc != -1);
- }
- dataReady = true;
- if(closeAfterRcev)close();
- }catch(Exception e){
- exception = e;
- if(alert)e.printStackTrace();
- }
- }
- public byte[] getData(){
- return ba.toByteArray();
- }
- public void waitforData(){
- try {
- while(!dataReady && exception==null){
- Thread.sleep(10);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void waitforHeader(){
- try {
- while(!headerReady && exception==null){
- Thread.sleep(10);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void waitforData(long millis){
- try {
- while(!dataReady && exception==null && millis>=0 ){
- millis -= 10;
- Thread.sleep(10);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void waitforHeader(long millis){
- try {
- while(!headerReady && exception==null && millis>=0 ){
- millis -= 10;
- Thread.sleep(10);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- @SuppressWarnings("deprecation")
- public void close() {
- try {
- this.stop();
- is.close();
- } catch (IOException e) {
- }
- }
- }
- class TimeoutTimer extends Thread{
- HTTPHeaderScanner htsc;
- int time1;
- int time2;
- public TimeoutTimer(HTTPHeaderScanner HTTPHeaderScanner,int time) {
- this.htsc = HTTPHeaderScanner;
- this.time1 = time;
- this.time2 = time;
- }
- public void run() {
- try {
- while(! (time1<0)){
- time1-=10;
- Thread.sleep(10);
- }
- if(htsc.exception == null)
- htsc.dataReady = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void reset(){
- time1 = time2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement