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
|
Index: src/com/prolixtech/jaminid/Connection.java
===================================================================
--- src/com/prolixtech/jaminid/Connection.java (revision 13)
+++ src/com/prolixtech/jaminid/Connection.java (working copy)
@@ -3,10 +3,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
@@ -82,7 +85,7 @@
CONNECTED:
while(true){
- StringBuffer requestInputBuffer = new StringBuffer();
+ List<Byte> requestInputBuffer = new ArrayList<Byte>();
int byteIn = -1;
int CRLFState = 0;
@@ -106,7 +109,7 @@
byteIn = socketInput.read();
if (byteIn > 0) {
- requestInputBuffer.append((char) byteIn);
+ requestInputBuffer.add((byte)byteIn);
if (doubleCRLFpassed)
bodyCharsParsed++;
@@ -142,11 +145,11 @@
if ("\n".charAt(0) == (thischar)) {
CRLFState++;
doubleCRLFpassed = true;
- serviceRequest.addRequestLines(requestInputBuffer
- .toString());
+ String inputString = bytesToString(requestInputBuffer);
+ serviceRequest.addRequestLines(inputString);
+
+ requestInputBuffer = new ArrayList<Byte>();
- requestInputBuffer = new StringBuffer();
-
expectedBodySize = serviceRequest.switchToBody();
} else {
CRLFState = 0;
@@ -163,8 +166,9 @@
&& bodyCharsParsed < expectedBodySize || doubleCRLFpassed
&& byteIn != -1 && socketInput.available() > 0);
- printlog("Request: " + requestInputBuffer.toString());
- serviceRequest.addRequestLines(requestInputBuffer.toString());
+ String inputString = bytesToString(requestInputBuffer);
+ printlog("Request: " + inputString);
+ serviceRequest.addRequestLines(inputString);
serviceRequest.switchToCompleted();
@@ -242,6 +246,22 @@
}
/**
+ * Added dnaber 2007-06-09, to make UTF-8 input work.
+ */
+ private String bytesToString(List<Byte> requestInputBuffer) {
+ Byte[] b = (Byte[])requestInputBuffer.toArray(new Byte[]{});
+ byte[] bytes = new byte[b.length];
+ for (int i = 0; i < b.length; i++) {
+ bytes[i] = b[i];
+ }
+ try {
+ return new String(bytes, "utf-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e.toString(), e);
+ }
+ }
+
+ /**
* a shortcut method
*
* @param message
@@ -261,7 +281,7 @@
protected void sendString(Object string) throws IOException {
if (string == null)
return;
- socketOutput.write((string.toString()).getBytes());
+ socketOutput.write((string.toString()).getBytes("UTF-8"));
}
protected void sendString(byte[] bytes) {
Index: src/com/prolixtech/jaminid/Protocol.java
===================================================================
--- src/com/prolixtech/jaminid/Protocol.java (revision 13)
+++ src/com/prolixtech/jaminid/Protocol.java (working copy)
@@ -280,15 +280,16 @@
MIME.setProperty(".txt", "text/plain");
-
+ /* dnaber, 2006-09-16
try {
OutputStream nmim = new FileOutputStream(Protocol.MIMEFILE);
MIME.storeToXML(nmim, "\nJAMINID MIMETYPES\n\nThese are the default MIME types for the jaminid daemon. If you had a modified but invalid MIME file here, it may have been reverted to this.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
- }
+ }*/
+
}
}
\ No newline at end of file
Index: src/com/prolixtech/jaminid/Daemon.java
===================================================================
--- src/com/prolixtech/jaminid/Daemon.java (revision 13)
+++ src/com/prolixtech/jaminid/Daemon.java (working copy)
@@ -4,6 +4,7 @@
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketException;
import com.prolixtech.utils.SingletonLogger;
import com.prolixtech.utils.Suspender;
@@ -163,12 +164,19 @@
}
} finally {
- serverSocket.close();
+ if (!serverSocket.isClosed()) {
+ serverSocket.close();
+ }
}
} catch (Exception e) {
- this.printlog("A major error has occured\n" + e);
- System.exit(-1);
+ if (RUNNING) {
+ final String message = "A major error has occured\n" + e;
+ System.out.println(message);
+ this.printlog(message);
+ } else {
+ // okay to ignore, happens wehn we call serverSocket.close()
+ }
}
}
@@ -202,6 +210,19 @@
*/
public void tearDown() {
RUNNING = false;
+ if (serverSocket != null && !serverSocket.isClosed()) {
+ try {
+ serverSocket.close();
+ } catch (IOException e) {
+ // okay to ignore
+ }
+ }
+ super.stop();
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
}
/**
Index: src/com/prolixtech/jaminid/Request.java
===================================================================
--- src/com/prolixtech/jaminid/Request.java (revision 13)
+++ src/com/prolixtech/jaminid/Request.java (working copy)
@@ -321,8 +321,8 @@
try {
String hex = s.substring(lastIndex + 1, lastIndex + 3);
char hin = (char) hex2int(hex);
- System.out.println(lastIndex + " Hex is " + hex + " or "
- + hin);
+ //System.out.println(lastIndex + " Hex is " + hex + " or "
+ // + hin);
String sBefore = s.substring(0, lastIndex);
String sAfter = s.substring(lastIndex + 3);
|