Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4df2e92be144a139cb7e739db027b5568acf815b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
 * Copyright (c) 1999, 2005 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import javax.servlet.ServletException;
import org.eclipse.equinox.socket.SocketInterface;

/**
 * The class provide a thread for processing HTTP requests.
 */
public class HttpThread extends Thread {
	/** Master HTTP object */
	protected Http http;

	/** if true this thread must terminate */
	protected volatile boolean running;

	/** Pool to which this thread belongs. */
	protected HttpThreadPool pool;

	/** socket that this thread is operating */
	private SocketInterface socket;

	/** Listener this thread is working for */
	private HttpListener listener;

	/** if true, we support Keep-Alive for the socket */
	private boolean supportKeepAlive;

	/**
	 * HttpThread constructor.
	 */
	public HttpThread(Http http, HttpThreadPool pool, String name) {
		super(pool, name);

		this.http = http;
		this.pool = pool;

		setDaemon(true); /* mark thread as daemon thread */
	}

	/**
	 * Returns true if this thread has been closed.
	 * @return boolean
	 */
	public boolean isClosed() {
		return (!running);
	}

	/**
	 * Close this thread.
	 */
	public synchronized void close() {
		running = false;

		if (socket == null) {
			interrupt();
		} else {
			try {
				socket.close();
			} catch (IOException e) {
				// TODO: consider logging
			}
		}
	}

	/**
	 * recall this thread.
	 */
	public synchronized void recall() {
		if (Http.DEBUG) {
			http.logDebug(getName() + ": recall on socket: " + socket); //$NON-NLS-1$

		}

		if ((socket != null) && !socket.isActive()) {
			try {
				if (Http.DEBUG) {
					http.logDebug(getName() + ": Closing socket: " + socket); //$NON-NLS-1$
				}

				socket.close();
			} catch (IOException e) {
				// TODO: consider logging
			}
		}

		supportKeepAlive = false;
	}

	public synchronized void handleConnection(HttpListener listenerParam, SocketInterface socketParam, int socketTimeout) {
		if (running) {
			this.listener = listenerParam;
			this.socket = socketParam;

			if (socketTimeout > 0) {
				try {
					socketParam.setSoTimeout(socketTimeout);

					supportKeepAlive = true;
				} catch (SocketException e) {
					supportKeepAlive = false;
				}
			} else {
				supportKeepAlive = false;
			}

			notify();
		}
	}

	public void run() {
		running = true;

		while (running) {
			if (socket == null) {
				/*
				 synchronized (this)
				 {
				 pool.putThread(this);

				 * Synchronizing on this before putThread
				 * causes deadlock when security manager causes
				 * Thread.isAlive() calls
				 */

				pool.putThread(this);

				synchronized (this) {
					try {
						wait();
					} catch (InterruptedException e) {
						// ignore and check exit condition
					}
				}
			}

			if (running && (socket != null)) {
				boolean keepAlive = false;

				try {
					if (Http.DEBUG) {
						http.logDebug(getName() + ": Processing request on socket: " + socket); //$NON-NLS-1$
					}

					socket.markInactive();

					listener.handleConnection(socket);

					keepAlive = supportKeepAlive && !socket.isClosed();
				} catch (InterruptedIOException e) {
					/* A read on the socket did not complete within the timeout period.
					 */
					keepAlive = false;

					if (Http.DEBUG) {
						http.logDebug(getName() + ": Read Timeout while processing connection on socket: " + socket, e); //$NON-NLS-1$
					}
				} catch (SocketException e) {
					/* Most likely the user agent closed the socket.
					 */
					keepAlive = false;

					if (Http.DEBUG) {
						http.logDebug(getName() + ": Socket Exception while processing connection on socket: " + socket, e); //$NON-NLS-1$
					}
				}
				// BUGBUG Need to handle UnavailableException
				// Servlet 2.2 Section 3.3.3.2
				// BUGBUG An unhandled exception should result in flushing the response
				// buff and returning status code 500.
				// Servlet 2.3 Section 9.9.2
				catch (ServletException e) {
					/* The Servlet threw a ServletException.
					 */
					keepAlive = false;

					http.logWarning(HttpMsg.HTTP_SERVLET_EXCEPTION, e);
				} catch (IOException e) {
					/* The Servlet threw an IOException.
					 */
					keepAlive = false;

					http.logWarning(HttpMsg.HTTP_CONNECTION_EXCEPTION, e);
				} catch (Throwable t) {
					/* Some exception has occurred. Log it and keep
					 * the thread working.
					 */
					keepAlive = false;

					http.logError(HttpMsg.HTTP_CONNECTION_EXCEPTION, t);
				} finally {
					if (!keepAlive) {
						if (!socket.isClosed()) {
							try {
								if (Http.DEBUG) {
									http.logDebug(getName() + ": Closing socket: " + socket); //$NON-NLS-1$
								}

								socket.close();
							} catch (IOException e) {
								// TODO: consider logging
							}
						}

						socket = null;
						listener = null;
					}
				}
			}
		}
	}
}

Back to the top