Index: WebCore/websockets/WebSocketRequest.cpp |
diff --git a/WebCore/websockets/WebSocketRequest.cpp b/WebCore/websockets/WebSocketRequest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1e2b994c953ce5d4cfd49d009f1273cff97e400f |
--- /dev/null |
+++ b/WebCore/websockets/WebSocketRequest.cpp |
@@ -0,0 +1,146 @@ |
+/* |
+ * Copyright (C) 2009 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "WebSocketRequest.h" |
+ |
+#include "KURL.h" |
+#include "StringBuilder.h" |
+ |
+namespace WebCore { |
+ |
+WebSocketRequest::WebSocketRequest(const KURL& url, const String& protocol, const String& origin) |
+ : m_url(url) |
+ , m_protocol(protocol) |
+ , m_secure(m_url.protocolIs("wss")) |
+ , m_origin(origin) |
+{ |
+} |
+ |
+WebSocketRequest::~WebSocketRequest() |
+{ |
+} |
+ |
+const KURL& WebSocketRequest::url() const |
+{ |
+ return m_url; |
+} |
+ |
+void WebSocketRequest::setURL(const KURL& url) |
+{ |
+ m_url = url; |
+} |
+ |
+const String WebSocketRequest::host() const |
+{ |
+ return m_url.host().lower(); |
+} |
+ |
+const String& WebSocketRequest::protocol() const |
+{ |
+ return m_protocol; |
+} |
+ |
+void WebSocketRequest::setProtocol(const String& protocol) |
+{ |
+ m_protocol = protocol; |
+} |
+ |
+bool WebSocketRequest::secure() const |
+{ |
+ return m_secure; |
+} |
+ |
+void WebSocketRequest::setSecure(bool secure) |
+{ |
+ m_secure = secure; |
+} |
+ |
+const String& WebSocketRequest::origin() const |
+{ |
+ return m_origin; |
+} |
+ |
+void WebSocketRequest::setOrigin(const String& origin) |
+{ |
+ m_origin = origin; |
+} |
+ |
+String WebSocketRequest::location() const |
+{ |
+ StringBuilder builder; |
+ if (m_secure) { |
+ builder.append("wss"); |
+ } else { |
+ builder.append("ws"); |
+ } |
+ builder.append("://"); |
+ builder.append(host()); |
+ if ((!m_secure && m_url.port() != 81) || (m_secure && m_url.port() != 815)) { |
+ builder.append(":"); |
+ builder.append(String::number(m_url.port())); |
+ } |
+ builder.append(m_url.path()); |
+ return builder.toString(); |
+} |
+ |
+CString WebSocketRequest::flattenToString() const |
+{ |
+ StringBuilder builder; |
+ |
+ builder.append("GET "); |
+ builder.append(url().path()); |
+ if (!url().query().isEmpty()) { |
+ builder.append("?"); |
+ builder.append(url().query()); |
+ } |
+ if (url().hasRef()) { |
+ builder.append("#"); |
+ builder.append(url().ref()); |
+ } |
+ builder.append(" HTTP/1.1\r\n"); |
+ builder.append("Upgrade: WebSocket\r\n"); |
+ builder.append("Connection: Upgrade\r\n"); |
+ builder.append("Host: "); |
+ builder.append(host()); |
+ builder.append("\r\n"); |
+ builder.append("Origin: "); |
+ builder.append(origin()); |
+ builder.append("\r\n"); |
+ if (!protocol().isEmpty()) { |
+ builder.append("WebSocket-Protocol: "); |
+ builder.append(protocol()); |
+ builder.append("\r\n"); |
+ } |
+ // FIXME: add auth info |
+ builder.append("\r\n"); |
+ return builder.toString().utf8(); |
+} |
+ |
+} // namespace WebCore |