001: // GET http://localhost/1.zip HTTP/1.1
002: //Host: localhost
003: //Accept: */*
004: //Referer: http://localhost
005: //User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
006: //Pragma: no-cache
007: //Cache-Control: no-cache
008: //Connection: close
009:
010: //HTTP/1.1 200 OK
011: //Server: Microsoft-IIS/5.1
012: //Connection: close
013: //Date: Sun, 01 Jun 2003 13:28:01 GMT
014: //Content-Type: application/x-zip-compressed
015: //Accept-Ranges: bytes
016: //Last-Modified: Fri, 30 May 2003 05:37:04 GMT
017: //ETag: "08029806d26c31:847"
018: //Content-Length: 927645
019:
020: //GET http://localhost/1.zip HTTP/1.1
021: //Host: localhost
022: //Accept: */*
023: //Referer: http://localhost
024: //User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
025: //Range: bytes=268037-
026: //Pragma: no-cache
027: //Cache-Control: no-cache
028: //Connection: close
029:
030: //HTTP/1.1 206 Partial content
031: //Server: Microsoft-IIS/5.1
032: //Connection: close
033: //Date: Sun, 01 Jun 2003 13:28:56 GMT
034: //Content-Type: application/x-zip-compressed
035: //Last-Modified: Fri, 30 May 2003 05:37:04 GMT
036: //ETag: "08029806d26c31:847"
037: //Content-Length: 659608
038: //Content-Range: bytes 268037-927644/927645
039:
040: package vicazh.hyperpool.stream.net.http;
041:
042: import java.io.*;
043: import java.util.*;
044: import vicazh.hyperpool.stream.*;
045:
046: class ReconnectServerStream extends ServerStream {
047: ReconnectServerStream(Session session, OutputStream outputstream) {
048: super (session, outputstream);
049: }
050:
051: public void head(String version, int code, String message)
052: throws IOException {
053: super .head(version, code, message);
054: if (((ReconnectConnection) session.connection).accept
055: && code != PARTIAL_CONTENT) {
056: ((ReconnectConnection) session.connection).accept = false;
057: throw new BreakException();
058: }
059: }
060:
061: private boolean b;
062:
063: public void field(String s1, String s2) throws IOException {
064: super .field(s1, s2);
065: if (((ReconnectConnection) session.connection).accept)
066: return;
067: if (s1.equalsIgnoreCase("Accept-Ranges")
068: && s2.startsWith("bytes"))
069: b = true;
070: else if (s1.equalsIgnoreCase("Content-Range")
071: && s2.startsWith("bytes")) {
072: StringTokenizer st = new StringTokenizer(s2);
073: st.nextToken();
074: ((ReconnectConnection) session.connection).index = Long
075: .parseLong(st.nextToken("-").trim());
076: b = true;
077: } else if (s1.equalsIgnoreCase("Content-Length"))
078: ((ReconnectConnection) session.connection).length = Long
079: .parseLong(s2.trim());
080: }
081:
082: private Timer timer;
083:
084: public void header() throws IOException {
085: super .header();
086: outputstream = ((ReconnectConnection) connection).serverout;
087: if (session.getClient().getMethod() != null
088: && session.getClient().getMethod().equalsIgnoreCase(
089: "get")
090: && b
091: && (((ReconnectConnection) session.connection).length > 0
092: || getCode() == OK || getCode() == PARTIAL_CONTENT)) {
093: ((ReconnectConnection) connection).length += ((ReconnectConnection) connection).index;
094: ((ReconnectConnection) connection).accept = true;
095: }
096: if (!((ReconnectConnection) connection).accept)
097: return;
098: timer = new Timer();
099: timer.schedule(new TimerTask() {
100: private long indexsave;
101:
102: private long time;
103:
104: public void run() {
105: if (((ReconnectConnection) connection).index == indexsave) {
106: if (new Date().getTime() - time < ((ReconnectService) connection.element)
107: .getTimeout() * 1000)
108: return;
109: timer.cancel();
110: try {
111: ((ReconnectConnection) connection).clientout
112: .flush();
113: } catch (Exception e) {
114: }
115: try {
116: ((ReconnectConnection) connection).clientout
117: .close();
118: } catch (Exception e) {
119: }
120: } else {
121: indexsave = ((ReconnectConnection) connection).index;
122: time = new Date().getTime();
123: }
124: }
125: }, 1000, 1000);
126: }
127:
128: public void content(int i) throws IOException {
129: ((ReconnectConnection) session.connection).index++;
130: super .content(i);
131: }
132:
133: public void end() {
134: if (timer != null)
135: timer.cancel();
136: super .end();
137: }
138:
139: public void close() throws IOException {
140: if (timer != null)
141: timer.cancel();
142: if (((ReconnectConnection) session.connection).check(session))
143: super.close();
144: }
145: }
|