Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: plugins/org.chromium.sdk.wipbackend.wk118685/src/org/chromium/sdk/internal/websocket/HandshakeUtil.java

Issue 11829027: drop old backends (Closed) Base URL: https://chromedevtools.googlecode.com/svn/trunk
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.sdk.internal.websocket;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.net.InetSocketAddress;
11 import java.nio.charset.Charset;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 /**
17 * Methods, classes and constants used in all WebSocket handshake procedures.
18 */
19 public class HandshakeUtil {
20 public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
21 public static final Charset ASCII_CHARSET = Charset.forName("ASCII");
22
23 static ArrayList<String> createHttpFields(InetSocketAddress endpoint) {
24 ArrayList<String> fields = new ArrayList<String>();
25 fields.add("Connection: Upgrade");
26 return fields;
27 }
28
29 static void checkOriginString(String origin) {
30 for (int i = 0; i < origin.length(); i++) {
31 char ch = origin.charAt(i);
32 if (ch >= 'A' && ch <= 'Z') {
33 throw new IllegalArgumentException();
34 }
35 }
36 }
37
38 public interface HttpResponse {
39 int getCode();
40 Map<String, String> getFields();
41 String getReasonPhrase();
42 }
43
44 public static HttpResponse readHttpResponse(LineReader input) throws IOExcepti on {
45 final int code;
46 final String reasonPhrase;
47 // First line.
48 {
49 byte[] firstLine = input.readUpTo0x0D0A();
50 if (firstLine.length < 7 - 2) {
51 throw new IOException("Malformed response");
52 }
53 int space1Pos = byteIndexOf((byte)' ', firstLine, 0);
54 if (space1Pos == -1) {
55 throw new IOException("Malformed response");
56 }
57 int space2Pos = byteIndexOf((byte)' ', firstLine, space1Pos + 1);
58 if (space2Pos == -1) {
59 throw new IOException("Malformed response");
60 }
61 if (space2Pos - space1Pos != 4) {
62 throw new IOException("Malformed response");
63 }
64 int codeTemp = 0;
65 for (int i = space1Pos + 1; i < space2Pos; i++) {
66 codeTemp = codeTemp * 10 + firstLine[i] - (byte)'0';
67 }
68 code = codeTemp;
69 int reasonPhraseStart = space2Pos + 1;
70 reasonPhrase = new String(firstLine, reasonPhraseStart, firstLine.length - space2Pos - 1,
71 ASCII_CHARSET);
72 }
73
74 // Fields.
75 final Map<String, String> responseFields;
76 {
77 responseFields = new HashMap<String, String>();
78 while (true) {
79 byte[] line = input.readUpTo0x0D0A();
80 if (line.length == 0) {
81 break;
82 }
83 String lineStr = new String(line, UTF_8_CHARSET);
84 int colonPos = lineStr.indexOf(':');
85 if (colonPos == -1) {
86 throw new IOException("Malformed response field");
87 }
88 if (colonPos == 0) {
89 throw new IOException("Malformed response field: empty key");
90 }
91 String key = lineStr.substring(0, colonPos).toLowerCase();
92 if (lineStr.length() > colonPos + 1 && lineStr.charAt(colonPos + 1) == ' ') {
93 colonPos++;
94 }
95 String value = lineStr.substring(colonPos + 1);
96 Object conflict = responseFields.put(key, value);
97 if (conflict != null) {
98 throw new IOException("Malformed response field: duplicated field: " + key);
99 }
100 }
101 }
102 return new HttpResponse() {
103 @Override public int getCode() {
104 return code;
105 }
106 @Override public String getReasonPhrase() {
107 return reasonPhrase;
108 }
109 @Override public Map<String, String> getFields() {
110 return responseFields;
111 }
112 };
113 }
114
115 public static abstract class LineReader {
116 abstract byte[] readUpTo0x0D0A() throws IOException;
117 }
118
119 public static LineReader createLineReader(final InputStream input) {
120 return new LineReader() {
121 @Override
122 byte[] readUpTo0x0D0A() throws IOException {
123 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
124 while (true) {
125 // TODO(peter.rybin): this is slow (for connection logger implementati on).
126 int i = input.read();
127 if (i == -1) {
128 throw new IOException("End of stream");
129 }
130 byte b = (byte) i;
131 if (b == 0x0D) {
132 break;
133 }
134 if (b == 0x0A) {
135 throw new IOException("Malformed end of line");
136 }
137 outputStream.write(b);
138 }
139 {
140 int i = input.read();
141 if (i == -1) {
142 throw new IOException("End of stream");
143 }
144 byte b = (byte) i;
145 if (b != 0x0A) {
146 throw new IOException("Malformed end of line");
147 }
148 }
149 return outputStream.toByteArray();
150 }
151 };
152 }
153
154 private static int byteIndexOf(byte b, byte[] array, int start) {
155 int i = start;
156 while (i < array.length) {
157 if (array[i] == b) {
158 return i;
159 }
160 i++;
161 }
162 return -1;
163 }
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698