View | Details | Raw Unified | Return to bug 287302
Collapse All | Expand All

(-)src/org/eclipse/atf/mozilla/ide/ui/netmon/model/impl/MozHTTPRequest.java (-50 / +36 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.atf.mozilla.ide.ui.netmon.model.impl;
11
package org.eclipse.atf.mozilla.ide.ui.netmon.model.impl;
12
12
13
import org.eclipse.atf.mozilla.ide.ui.MozIDEUIPlugin;
13
import org.eclipse.atf.mozilla.ide.ui.netmon.model.IHTTPRequest;
14
import org.eclipse.atf.mozilla.ide.ui.netmon.model.IHTTPRequest;
14
import org.mozilla.interfaces.nsIHttpChannel;
15
import org.mozilla.interfaces.nsIHttpChannel;
15
import org.mozilla.interfaces.nsIHttpHeaderVisitor;
16
import org.mozilla.interfaces.nsIHttpHeaderVisitor;
Lines 30-129 Link Here
30
31
31
	//URL of the http request
32
	//URL of the http request
32
	protected String url;
33
	protected String url;
33
	
34
34
	//HTTP method value
35
	//HTTP method value
35
	protected String method;
36
	protected String method;
36
	
37
37
	//Body content of the request
38
	//Body content of the request
38
	protected String body = "";
39
	protected String body = "";
39
	
40
40
	//marks if this request is started using XHR
41
	//marks if this request is started using XHR
41
	protected boolean xhr = false;
42
	protected boolean xhr = false;
42
	
43
43
	public MozHTTPRequest( nsIHttpChannel httpChannel ){
44
	public MozHTTPRequest(nsIHttpChannel httpChannel) {
44
		this( httpChannel, false );
45
		this(httpChannel, false);
45
	}
46
	}
46
	
47
47
	public MozHTTPRequest( nsIHttpChannel httpChannel, boolean xhr ){
48
	public MozHTTPRequest(nsIHttpChannel httpChannel, boolean xhr) {
48
		//creating the local request representation
49
		//creating the local request representation
49
		
50
50
		this.xhr = xhr;
51
		this.xhr = xhr;
51
		
52
52
		//URL of the request
53
		//URL of the request
53
		this.url = httpChannel.getURI().getAsciiSpec();
54
		this.url = httpChannel.getURI().getAsciiSpec();
54
		
55
55
		//HTTP method used
56
		//HTTP method used
56
		this.method = httpChannel.getRequestMethod();
57
		this.method = httpChannel.getRequestMethod();
57
58
58
		extractBody(httpChannel);
59
		extractBody(httpChannel);
59
		
60
60
		extractHeaders(httpChannel);
61
		extractHeaders(httpChannel);
61
	}
62
	}
62
	
63
63
	/*
64
	/*
64
	 * Currently handling POSTs and GETs
65
	 * Currently handling POSTs and GETs
65
	 */
66
	 */
66
	protected void extractBody( nsIHttpChannel httpChannel ) {
67
	protected void extractBody(nsIHttpChannel httpChannel) {
67
		if ( POST_METHOD.equals(this.method) || PUT_METHOD.equals(this.method) ) {
68
		if (POST_METHOD.equals(this.method) || PUT_METHOD.equals(this.method)) {
68
			
69
69
			// set the POST body as the body
70
			// set the POST body as the body
70
			
71
71
			//The POST body needs to be read from an InputStream
72
			//The POST body needs to be read from an InputStream
72
			nsIScriptableInputStream scriptableIS = null;
73
			nsIScriptableInputStream scriptableIS = null;
73
			
74
74
			//need it so that we can rewind the stream back
75
			//need it so that we can rewind the stream back
75
			nsISeekableStream seekableStream = null;
76
			nsISeekableStream seekableStream = null;
76
			nsIInputStream is = null;
77
			nsIInputStream is = null;
77
			try {
78
			try {
78
				nsIUploadChannel upChannel = (nsIUploadChannel) httpChannel
79
				nsIUploadChannel upChannel = (nsIUploadChannel) httpChannel.queryInterface(nsIUploadChannel.NS_IUPLOADCHANNEL_IID);
79
						.queryInterface(nsIUploadChannel.NS_IUPLOADCHANNEL_IID);
80
80
81
				is = upChannel.getUploadStream();
81
				is = upChannel.getUploadStream();
82
				if (is != null) {
82
				if (is != null) {
83
					seekableStream= (nsISeekableStream) is.queryInterface(nsISeekableStream.NS_ISEEKABLESTREAM_IID);
83
					seekableStream = (nsISeekableStream) is.queryInterface(nsISeekableStream.NS_ISEEKABLESTREAM_IID);
84
84
85
					scriptableIS = (nsIScriptableInputStream) Mozilla
85
					scriptableIS = (nsIScriptableInputStream) Mozilla.getInstance().getComponentManager().createInstanceByContractID("@mozilla.org/scriptableinputstream;1", null, nsIScriptableInputStream.NS_ISCRIPTABLEINPUTSTREAM_IID);
86
							.getInstance()
87
							.getComponentManager()
88
							.createInstanceByContractID(
89
									"@mozilla.org/scriptableinputstream;1",
90
									null,
91
									nsIScriptableInputStream.NS_ISCRIPTABLEINPUTSTREAM_IID);
92
86
93
					scriptableIS.init(is);
87
					scriptableIS.init(is);
94
					
88
95
					//read the entire stream into a StringBuffer
89
					//read the entire stream into a StringBuffer
96
					StringBuffer postBuf = new StringBuffer();
90
					StringBuffer postBuf = new StringBuffer();
97
					long count;
91
					long count;
98
					while(  (count = scriptableIS.available()) > 0 )//some left to read{
92
					while ((count = scriptableIS.available()) > 0)
99
						postBuf.append( scriptableIS.read(count) );
93
						//some left to read{
100
					
94
						postBuf.append(scriptableIS.read(count));
101
					
95
102
					body = postBuf.toString();
96
					body = postBuf.toString();
103
						
97
104
				}
98
				}
105
			} catch (Exception e) {
99
			} catch (Exception e) {
106
				//Any errors here, the POST body will be empty
100
				//Any errors here, the POST body will be empty
107
			}
101
				MozIDEUIPlugin.log(e);
108
			finally{
102
			} finally {
109
				if( scriptableIS != null )
110
					scriptableIS.close();
111
				
112
				//rewind
103
				//rewind
113
				if( seekableStream != null ){
104
				if (seekableStream != null) {
114
					seekableStream.seek( 0, 0 );
105
					seekableStream.seek(nsISeekableStream.NS_SEEK_SET, 0);
115
				}
106
				}
116
				
117
				if( is != null )
118
					is.close();
119
			}
107
			}
120
		}
108
		} else {
121
		else{
122
			// set the body to the parameters in the URL
109
			// set the body to the parameters in the URL
123
			body = httpChannel.getURI().getPath();
110
			body = httpChannel.getURI().getPath();
124
		}
111
		}
125
	}
112
	}
126
	
113
127
	public String getBody() {
114
	public String getBody() {
128
		return body;
115
		return body;
129
	}
116
	}
Lines 140-147 Link Here
140
		return xhr;
127
		return xhr;
141
	}
128
	}
142
129
143
	protected void visitHeaders(nsIHttpChannel httpChannel,
130
	protected void visitHeaders(nsIHttpChannel httpChannel, nsIHttpHeaderVisitor visitor) {
144
			nsIHttpHeaderVisitor visitor) {
131
		httpChannel.visitRequestHeaders(visitor);
145
		httpChannel.visitRequestHeaders( visitor );		
146
	}
132
	}
147
}
133
}

Return to bug 287302