Compare commits

..

76 Commits

Author SHA1 Message Date
Daniel Hammond
0a023409b1 Fix crash on mac (and 32-bit iOS?) - don't represent SRDelegateAvailableMethods as a bitfield (#530)
* Don't represent SRDelegateAvailableMethods as a bitfield

Since Objective-C uses a signed char for BOOL on some platforms (macOS and 32-bit iOS) packing this into a bitfield doesn't work since when it tries to read the field it looks for the sign bit and fails with an EXC_BAD_INSTRUCTION. This change sacrifices a few bits for the extra portability.

* CR - followup
2018-04-28 10:43:15 -07:00
Richard Ross
28035e1a98 Deprecate SSL pinning and trust chain verification. (#534)
Oh boy. Here's a controversial change.
![](http://i.imgur.com/t8JjQix.gif)

Let's give a bit of backstory.

A few weeks ago, Facebook was contacted by a whitehat hacker (the good
guys) about a security vulnerability here in SocketRocket.

For those of you who are truly interested in what that security flaw
was, it is essentially the same flaw as outlined here:

https://www.synopsys.com/blogs/software-security/ineffective-certificate-pinning-implementations/

So, we were faced with a choice - quietly push out a patch, and hope
that eventually existing applications updated, or be transparent and
admit we screwed up.

This is us admititng we screwed up. And while yes, we could probably fix
the implementation. But we talked internally, and decided that the best
approach here is to completely remove the option for pinning.

For all of our existing users that use certificate pinning, while we
understand that in the past there has been a very large barrier to entry
with getting a CA to issue a certificate.

However, since the rollout of CAs like LetsEncrypt, there's become an
ever-dwindling reason to actually use self-signed or unsigned
certificates.

For this reason, we're going to go ahead and deprecate the APIs that
allow SSL pinning and disabling trust chain verification. The pinning
APIs are now going to throw an exception when invoked, and the trust
chain APIs have deprecation warnings.

If you are a user of these APIs, and you for some reason **CANNOT** use
a trust chain validated certificate, PLEASE speak up. While we cannot
think of any reason to use those kinds of certificates, it's entirely
possible we overlooked something. We'll leave this pullrequest unmerged
for a two week period (Monday, August 28th, 2017), at which point,
unless we have feedback convincing us otherwise, we will go ahead with
this change.
2017-08-30 14:19:48 -07:00
Erik Price
877ac7438b Make copying optional when receiving data (#428)
* Reduce memory usage

Reduce memory usage by discarding, not resetting, the frame data buffer.
Let delegate control copying.

* Delegate methods expect `self`

* Add `-sendWithoutCopyingData:error:`

* Add `-webSocket:shouldCopyReceivedData:`

* Fix error messages

* Remove `-webSocket:shouldCopyReceivedData:`

* Revert "Fix error messages"

This reverts commit 4d5f5f018d0c953c9f9c9394f284f8358a039f90.

* Fix typo

* Copy only control frames

* Fix error message
2016-10-31 11:17:35 -07:00
Enrique Osuna
20686b9990 Ensure test environment is ready (#462)
When running tests, lets make sure that the test environment is ready to go.
2016-10-31 11:16:31 -07:00
woudini
41b57bb2fc Fix memory leak by freeing mutex. (#469) 2016-09-14 00:08:27 -07:00
Nikita Lutsenko
04b28a7c56 Add optional delegate method for received ping. (#463) 2016-08-25 16:41:07 -07:00
Nikita Lutsenko
d09212793c Merge pull request #461 from facebook/nlutsenko.lint
Lint, cleanup and fix analyzer warning.
2016-08-24 13:35:34 -07:00
Nikita Lutsenko
eff1db8da7 Fix missing prototypes for C functions. 2016-08-23 17:20:15 -07:00
Nikita Lutsenko
103dc2623b Cleanup and update summary for podspec. 2016-08-23 14:57:52 -07:00
Nikita Lutsenko
45f03ed9b9 Fix static analyzer warning in SRProxyConnect. 2016-08-23 14:53:19 -07:00
Nikita Lutsenko
35e965f2c5 Update xctoolchain to latest. 2016-08-23 14:53:19 -07:00
Nikita Lutsenko
3e5be70f1b Lint and cleanup source files. 2016-08-23 14:53:13 -07:00
Nikita Lutsenko
fc0aad7844 Remove no longer relevant Makefile. 2016-08-23 14:53:03 -07:00
Nikita Lutsenko
00a8a66240 Merge pull request #455 from facebook/nlutsenko.proxy.crash
Optimize input queue processing in SRProxyConnect.
2016-08-22 17:42:44 -07:00
Nikita Lutsenko
47971ebe5e Merge pull request #460 from facebook/nlutsenko.warning
Fix warnings on few potential clang configurations.
2016-08-22 16:06:16 -07:00
Nikita Lutsenko
9c2201f099 Cleanup proxy connect reference when socket is opened asynchronously. 2016-08-22 15:23:50 -07:00
Nikita Lutsenko
88cdc0b586 Optimize input queue processing in SRProxyConnect. 2016-08-22 15:23:50 -07:00
Nikita Lutsenko
ee6dd82992 Remove default: case in switch where all possible values are handled. 2016-08-22 15:12:18 -07:00
Nikita Lutsenko
abea58207e Fix nullability analysis in SRHTTPConnectMessage. 2016-08-22 15:11:58 -07:00
Nikita Lutsenko
cccbf71a7b Merge pull request #453 from facebook/nlutsenko.cleanup
Update configurations and unbreak compilation of a sample app.
2016-08-08 12:57:27 -07:00
Nikita Lutsenko
682be00378 Unbreak compilation of TestChat app. 2016-08-08 10:46:35 -07:00
Nikita Lutsenko
85dd530191 Update xctoolchain to latest. 2016-08-08 10:45:19 -07:00
Nikita Lutsenko
691241f102 Extract and cleanup setting network service type on input/output streams. 2016-08-08 10:45:17 -07:00
Fjölnir Ásgeirsson
ff03b396a0 Fixed build on under iOS 10 SDK (#450)
iOS 10 adds NSURLNetworkServiceTypeCallSignaling
which made this switch in
setupNetworkServiceType non-exhaustive
2016-07-29 11:54:47 -07:00
Nikita Lutsenko
471b99ccde Remove pages submodule reference. (#452) 2016-07-28 15:59:14 -07:00
Nikita Lutsenko
130967bcb9 Unify usage of default buffer size between SRWebSocket and SRProxyConnect. (#449) 2016-07-28 14:38:02 -07:00
Nikita Lutsenko
ecf7f75de3 Force linking of object files for built-in Foundation categories. (#441) 2016-07-08 18:33:15 -07:00
Richard Ross
4184f74f18 Make payload masking use vector instructions. (#440)
This is up to 50x faster when running on an ARM64 device, and effects
every payload we send out from the device.
2016-07-08 15:12:33 -07:00
Nikita Lutsenko
0632997fe7 Merge pull request #439 from mortonfox/patch-1
Update link to license
2016-07-08 11:46:02 -07:00
Morton Fox
84424d470a Update link to license 2016-07-08 12:30:48 -04:00
Nikita Lutsenko
f2e6387948 Improve type-safety on sending frames and remove extraneous class checks. (#438) 2016-07-07 19:53:15 -07:00
Nikita Lutsenko
ab462e597c Merge pull request #437 from facebook/nlutsenko.configurations
Update all targets to use shared configuration files from xctoolchain.
2016-07-06 13:24:02 -07:00
Nikita Lutsenko
e698ab30eb Fix undefined behavior on sending/reading the data. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
b666a4559a Remove usage of deprecated Endian with CFSwap. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
676948f555 Update Travis-CI configuration for new target names. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
eda51aba1b Fix all new warnings. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
a944d5fcbb Fixed documentation for SRWebSocketDelegate. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
e84a8fd596 Update all targets to shared configurations from xctoolchain. 2016-07-06 12:28:29 -07:00
Nikita Lutsenko
4605a70d61 Add xctoolchain submodule. 2016-07-05 19:36:39 -07:00
Nikita Lutsenko
2abdcb9614 Fix errors in TestChat application. (#431) 2016-07-01 17:51:15 -07:00
Nikita Lutsenko
71a52a57a2 Update README and CONTRIBUTING to Markdown and new documentation. (#432) 2016-07-01 14:58:33 -07:00
Michael Kirk
8096fef47d Pluggable, more flexible, security policies. (#429)
Extract @fredericjacobs' CertificateVerifier concept with @nlutsenko's
SRSecurityOptions into a pluggable SRSecurityPolicy model

This retains existing SSL configuration code paths, while allowing users
more flexibility to specify their own security policy.

If you are alread using AFNetworking and an `AFSecurityPolicy`, it's
intended that you can share domain trust logic by delegating
`SRSecurityPolicy evaluateTrust:ForDomain` to your AFSecurityPolicy
instance.

Inspired by original "Require TLS 1.2 & enable pinning" pull request by
Frederic Jacobs (@fredericjacobs) at:

https://github.com/facebook/SocketRocket/pull/274/files
2016-06-30 20:25:06 -07:00
Michael Kirk
b4e7932a59 specify ruby version for CI (#430)
CI is currently failing because the default ruby on travis.org is 2.0,
but a dependency xcpretty, requires the latest activesupport, which
requires ruby >=2.2
2016-06-30 17:09:35 -07:00
Erik Price
fcd482898a Make copying optional when sending data (#427)
* Reduce memory usage

Reduce memory usage by discarding, not resetting, the frame data buffer.
Let delegate control copying.

* Delegate methods expect `self`

* Add `-sendWithoutCopyingData:error:`

* Add `-webSocket:shouldCopyReceivedData:`

* Update test script name in Makefile

* use a Swiftier name

* Fix error messages

* Remove `-webSocket:shouldCopyReceivedData:`

* Remove assertion

* Fix typo
2016-06-28 15:43:44 -07:00
Nikita Lutsenko
48465aeaae Update public APIs for Swift 3 API Guidelines. (#419) 2016-06-27 19:26:54 -07:00
Nikita Lutsenko
16abb5debd Use error pointers instead of asserts for validating status on send:. (#416) 2016-06-27 18:32:08 -07:00
Nikita Lutsenko
e22b67e56a Make readyState KV Observable and fully thread-safe. (#420) 2016-06-27 17:27:58 -07:00
Nikita Lutsenko
52017adfb4 Improve SRStatusCode import into Swift. (#418) 2016-06-27 17:23:55 -07:00
Nikita Lutsenko
29d8e0e832 Unify logging in SRWebSocket, SRProxyConnect. (#417) 2016-06-27 15:20:32 -07:00
Nikita Lutsenko
0af058c221 Merge pull request #415 from facebook/nlutsenko.cleanup
Split creation of CFHTTPMessage and random data into separate source files.
2016-06-17 15:06:14 -07:00
Nikita Lutsenko
86df623629 Split creation of random data into SRRandom. 2016-06-17 14:45:59 -07:00
Nikita Lutsenko
d3beb35574 Split connect CFHTTPMessage creation into SRHTTPConnectMessage. 2016-06-17 13:17:23 -07:00
Nikita Lutsenko
8dafa6908a Cleanup and add few sections to SRWebSocket. 2016-06-17 13:17:23 -07:00
Nikita Lutsenko
aaab4836c3 Merge pull request #414 from facebook/nlutsenko.proxy.crash
Fix crash on SRProxyConnect deallocation, when timeout was encountered.
2016-06-16 21:36:43 -07:00
Nikita Lutsenko
14d35acf6c Explicitly remove delegates, close streams if ProxyConnect is deallocated before full open. 2016-06-15 19:03:38 -07:00
Nikita Lutsenko
dc0b2a7ff6 Replace NSOperationQueue with dispatch_queue in SRProxyConnect. 2016-06-15 18:57:54 -07:00
Nikita Lutsenko
49ab8461b7 Reset delegates on streams if proxy connect failed with error. 2016-06-15 18:45:51 -07:00
Nikita Lutsenko
e17d8ce173 Merge pull request #411 from dharmeshkakadia/patch-1
Fixing broken links in README
2016-06-13 18:18:53 -07:00
Nikita Lutsenko
50399656e5 Fix compilation warnings when building with Xcode 8. (#412) 2016-06-13 17:55:24 -07:00
Dharmesh Kakadia
d6934198a3 Fixing broken links in README 2016-06-12 22:54:11 -07:00
Nikita Lutsenko
e404333501 Merge pull request #407 from facebook/nlutsenko.tests
Enable Autobahn (iOS only) and Carthage tests on Travis-CI.
2016-06-10 15:43:22 -07:00
Nikita Lutsenko
c1dc8647ce Enable autobahn and carthage tests on Travis-CI for iOS. 2016-06-09 21:29:05 -07:00
Nikita Lutsenko
d6ed65d084 Do not open autobahn test results automatically after testing is done. 2016-06-09 16:20:43 -07:00
Nikita Lutsenko
7d0593f1df Add missing SRProxyConnect to all framework targets. 2016-06-09 16:20:43 -07:00
Nikita Lutsenko
8e203e15c2 Remove inaccessible resource file from sample app. 2016-06-09 16:20:43 -07:00
Nikita Lutsenko
8c41a73038 Cleanup TCAppDelegate. 2016-06-09 16:20:43 -07:00
Nikita Lutsenko
56e471ae94 Remove useless import in SRWebSocket.h 2016-06-09 16:20:43 -07:00
Nikita Lutsenko
510d1b1bae Remove CocoaPods version requirement from Gemfile. 2016-06-08 20:28:34 -07:00
Nikita Lutsenko
06b8e936eb Add xcpretty to Gemfile. 2016-06-08 20:23:32 -07:00
Nikita Lutsenko
5ce5f5d1d8 Fix pinned certificates and extract SSL-related things into SRSecurityOptions. (#406) 2016-06-08 20:14:22 -07:00
Nikita Lutsenko
8065053898 Remove usage of deprecated APIs and cleanup TestChat app. (#404) 2016-06-08 20:12:46 -07:00
Nikita Lutsenko
f239c15ed4 Merge pull request #403 from facebook/nlutsenko.tests
Fix and improve autobahn test running on any machine.
2016-06-08 20:11:21 -07:00
Nikita Lutsenko
840c9050c6 Fix and update test running from Xcode. 2016-06-08 02:08:31 -07:00
Nikita Lutsenko
7e97284178 Remove no longer used checked in copy of virtualenv. 2016-06-08 02:08:10 -07:00
Nikita Lutsenko
1f122e7de5 Remove no longer used sr-testharness. 2016-06-08 02:07:49 -07:00
Nikita Lutsenko
c25405ec4b Unbreak test scripts to download python virtualenv and set it up properly. 2016-06-08 02:07:07 -07:00
78 changed files with 2525 additions and 4072 deletions

6
.gitmodules vendored
View File

@ -1,3 +1,3 @@
[submodule "pages"]
path = pages
url = git://github.com/square/SocketRocket.git
[submodule "Vendor/xctoolchain"]
path = Vendor/xctoolchain
url = https://github.com/ParsePlatform/xctoolchain.git

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
2.3.1

View File

@ -2,29 +2,42 @@ branches:
only:
- master
language: objective-c
os: osx
osx_image: xcode7.3
sudo: false
xcode_project: SocketRocket.xcodeproj
env:
global:
- IOS_SDK=iphonesimulator9.3
- IOS_SCHEME="SocketRocket-iOS"
- MACOS_SDK=macosx10.11
- MACOS_SCHEME="SocketRocketOSX"
- TVOS_SDK=appletvsimulator9.2
- TVOS_SCHEME="SocketRocket-tvOS"
matrix:
- DESTINATION="OS=9.3,name=iPad 2" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=9.3,name=iPad Air" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=9.3,name=iPhone 5" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=9.3,name=iPhone 6s" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=8.4,name=iPhone 6" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=8.4,name=iPad Air" SDK="$IOS_SDK" SCHEME="$IOS_SCHEME"
- DESTINATION="OS=9.2,name=Apple TV 1080p" SDK="$TVOS_SDK" SCHEME="$TVOS_SCHEME"
- DESTINATION="platform=OS X" SDK="$MACOS_SDK" SCHEME="$MACOS_SCHEME"
before_script:
- bundle install
- TEST_TYPE=iOS
- TEST_TYPE=OSX
- TEST_TYPE=tvOS
- TEST_TYPE=CocoaPods
- TEST_TYPE=Carthage
before_install:
- |
if [ "$TEST_TYPE" = iOS ] || [ "$TEST_TYPE" = OSX ] || [ "$TEST_TYPE" = tvOS ]; then
bundle install
elif [ "$TEST_TYPE" = Carthage ]; then
brew update
brew install carthage || brew upgrade carthage
fi
install:
- |
if [ "$TEST_TYPE" = iOS ]; then
./TestSupport/setup_env.sh .env
fi
script:
- xcodebuild -version
- xcodebuild -scheme "$SCHEME" -sdk "$SDK" -destination "$DESTINATION" -configuration Debug -PBXBuildsContinueAfterErrors=0 ACTIVE_ARCH_ONLY=0 build test | xcpretty -tc
- pod lib lint --verbose --fail-fast
- |
if [ "$TEST_TYPE" = iOS ]; then
set -o pipefail
xcodebuild -project SocketRocket.xcodeproj -scheme "SocketRocket-iOS" -sdk iphonesimulator build test
elif [ "$TEST_TYPE" = OSX ]; then
set -o pipefail
xcodebuild -project SocketRocket.xcodeproj -scheme "SocketRocket-macOS" -sdk macosx build | xcpretty -c
elif [ "$TEST_TYPE" = tvOS ]; then
set -o pipefail
xcodebuild -project SocketRocket.xcodeproj -scheme "SocketRocket-tvOS" -sdk appletvsimulator build | xcpretty -c
elif [ "$TEST_TYPE" = CocoaPods ]; then
pod lib lint SocketRocket.podspec
pod lib lint --use-libraries SocketRocket.podspec
elif [ "$TEST_TYPE" = Carthage ]; then
carthage build --no-skip-current
fi

View File

@ -1,9 +1,5 @@
# Contributing to SocketRocket
We want to make contributing to this project as easy and transparent as
possible.
## Our Development Process
... (in particular how this is synced with internal changes to the project)
We want to make contributing to this project as easy and transparent as possible.
## Pull Requests
We actively welcome your pull requests.
@ -16,8 +12,8 @@ We actively welcome your pull requests.
6. If you haven't already, complete the Contributor License Agreement ("CLA").
## Contributor License Agreement ("CLA")
In order to accept your pull request, we need you to submit a CLA. You only need
to do this once to work on any of Facebook's open source projects.
In order to accept your pull request, we need you to submit a CLA.
You only need to do this once to work on any of Facebook's open source projects.
Complete your CLA here: <https://code.facebook.com/cla>
@ -34,5 +30,4 @@ outlined on that page and do not file a public issue.
* Try to keep lines under 140 characters, if possible.
## License
By contributing to SocketRocket, you agree that your contributions will be licensed
under its BSD license.
By contributing to SocketRocket, you agree that your contributions will be licensed under its BSD license.

1
Configurations/Shared Symbolic link
View File

@ -0,0 +1 @@
../Vendor/xctoolchain/Configurations/

View File

@ -0,0 +1,17 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/iOS.xcconfig"
#include "Shared/Product/DynamicFramework.xcconfig"
PRODUCT_NAME = SocketRocket
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.ios
IPHONEOS_DEPLOYMENT_TARGET = 8.0
INFOPLIST_FILE = $(SRCROOT)/SocketRocket/Resources/Info.plist

View File

@ -0,0 +1,20 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/iOS.xcconfig"
#include "Shared/Product/StaticFramework.xcconfig"
PRODUCT_NAME = SocketRocket
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.ios
IPHONEOS_DEPLOYMENT_TARGET = 6.0
INFOPLIST_FILE = $(SRCROOT)/SocketRocket/Resources/Info.plist
OTHER_CFLAGS[sdk=iphoneos9.*] = $(inherited) -fembed-bitcode
OTHER_LDFLAGS = $(inherited) -Licucore

View File

@ -0,0 +1,17 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/macOS.xcconfig"
#include "Shared/Product/DynamicFramework.xcconfig"
PRODUCT_NAME = SocketRocket
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.macos
MACOSX_DEPLOYMENT_TARGET = 10.8
INFOPLIST_FILE = $(SRCROOT)/SocketRocket/Resources/Info.plist

View File

@ -0,0 +1,16 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/tvOS.xcconfig"
#include "Shared/Product/DynamicFramework.xcconfig"
PRODUCT_NAME = SocketRocket
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.tvos
INFOPLIST_FILE = $(SRCROOT)/SocketRocket/Resources/Info.plist

View File

@ -0,0 +1,19 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/iOS.xcconfig"
#include "Shared/Product/LogicTests.xcconfig"
PRODUCT_NAME = SocketRocketTests-iOS
PRODUCT_MODULE_NAME = SocketRocketTests
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.tests.ios
IPHONEOS_DEPLOYMENT_TARGET = 7.0
INFOPLIST_FILE = $(SRCROOT)/Tests/Resources/Info.plist

View File

@ -0,0 +1,19 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#include "Shared/Platform/iOS.xcconfig"
#include "Shared/Product/Application.xcconfig"
PRODUCT_NAME = TestChat
PRODUCT_MODULE_NAME = TestChat
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.socketrocket.testchat
IPHONEOS_DEPLOYMENT_TARGET = 8.0
INFOPLIST_FILE = $(SRCROOT)/TestChat/TestChat-Info.plist

View File

@ -1,3 +1,4 @@
source 'https://rubygems.org'
gem 'cocoapods', '0.39.0'
gem 'cocoapods'
gem 'xcpretty'

View File

@ -7,20 +7,24 @@ all:
clean:
$(MAKE) -C SocketRocket clean
test:
.env:
./TestSupport/setup_env.sh .env
test: .env
mkdir -p pages/results
bash ./TestSupport/run_test.sh $(TEST_SCENARIOS) $(TEST_URL) Debug || open pages/results/index.html && false
bash ./TestSupport/run_test_server.sh $(TEST_SCENARIOS) $(TEST_URL) Debug || open pages/results/index.html && false
open pages/results/index.html
test_all:
test_all: .env
mkdir -p pages/results
bash ./TestSupport/run_test.sh '*' $(TEST_URL) Debug || open pages/results/index.html && false
bash ./TestSupport/run_test_server.sh '*' $(TEST_URL) Debug || open pages/results/index.html && false
open pages/results/index.html
test_perf:
test_perf: .env
mkdir -p pages/results
bash ./TestSupport/run_test.sh '9.*' $(TEST_URL) Release || open pages/results/index.html && false
bash ./TestSupport/run_test_server.sh '9.*' $(TEST_URL) Release || open pages/results/index.html && false
open pages/results/index.html

213
README.md Normal file
View File

@ -0,0 +1,213 @@
# SocketRocket
![Platforms][platforms-svg]
[![License][license-svg]][license-link]
[![Podspec][podspec-svg]][podspec-link]
[![Carthage Compatible][carthage-svg]](carthage-link)
[![Build Status][build-status-svg]][build-status-link]
A conforming WebSocket ([RFC 6455](https://tools.ietf.org/html/rfc6455>)) client library for iOS, macOS and tvOS.
Test results for SocketRocket [here](http://facebook.github.io/SocketRocket/results/).
You can compare to what modern browsers look like [here](http://autobahn.ws/testsuite/reports/clients/index.html).
SocketRocket currently conforms to all core ~300 of [Autobahn](http://autobahn.ws/testsuite/>)'s fuzzing tests
(aside from two UTF-8 ones where it is merely *non-strict* tests 6.4.2 and 6.4.4).
## Features/Design
- TLS (wss) support, including self-signed certificates.
- Seems to perform quite well.
- Supports HTTP Proxies.
- Supports IPv4/IPv6.
- Supports SSL certificate pinning.
- Sends `ping` and can process `pong` events.
- Asynchronous and non-blocking. Most of the work is done on a background thread.
- Supports iOS, macOS, tvOS.
## Installing
There are a few options. Choose one, or just figure it out:
- **[CocoaPods](https://cocoapods.org)**
Add the following line to your Podfile:
```ruby
pod 'SocketRocket'
```
Run `pod install`, and you are all set.
- **[Carthage](https://github.com/carthage/carthage)**
Add the following line to your Cartfile:
```
github "facebook/SocketRocket"
```
Run `carthage update`, and you should now have the latest version of `SocketRocket` in your `Carthage` folder.
- **Using SocketRocket as a sub-project**
You can also include `SocketRocket` as a subproject inside of your application if you'd prefer, although we do not recommend this, as it will increase your indexing time significantly. To do so, just drag and drop the `SocketRocket.xcodeproj` file into your workspace.
## API
### `SRWebSocket`
The Web Socket.
#### Note:
`SRWebSocket` will retain itself between `-(void)open` and when it closes, errors, or fails.
This is similar to how `NSURLConnection` behaves (unlike `NSURLConnection`, `SRWebSocket` won't retain the delegate).
#### Interface
```objective-c
@interface SRWebSocket : NSObject
// Make it with this
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
// Set this before opening
@property (nonatomic, weak) id <SRWebSocketDelegate> delegate;
// Open with this
- (void)open;
// Close it with this
- (void)close;
// Send a Data
- (void)sendData:(nullable NSData *)data error:(NSError **)error;
// Send a UTF8 String
- (void)sendString:(NSString *)string error:(NSError **)error;
@end
```
### `SRWebSocketDelegate`
You implement this
```objective-c
@protocol SRWebSocketDelegate <NSObject>
@optional
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(NSString *)string;
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithData:(NSData *)data;
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(nullable NSString *)reason wasClean:(BOOL)wasClean;
@end
```
## Testing
Included are setup scripts for the python testing environment.
It comes packaged with vitualenv so all the dependencies are installed in userland.
To run the short test from the command line, run:
```bash
make test
```
To run all the tests, run:
```bash
make test_all
```
The short tests don't include the performance tests
(the test harness is actually the bottleneck, not SocketRocket).
The first time this is run, it may take a while to install the dependencies. It will be smooth sailing after that.
You can also run tests inside Xcode, which runs the same thing, but makes it easier to debug.
- Choose the `SocketRocket` target
- Run the test action (`⌘+U`)
### TestChat Demo Application
SocketRocket includes a demo app, TestChat.
It will "chat" with a listening websocket on port 9900.
#### TestChat Server
The sever takes a message and broadcasts it to all other connected clients.
It requires some dependencies though to run.
We also want to reuse the virtualenv we made when we ran the tests.
If you haven't run the tests yet, go into the SocketRocket root directory and type:
```bash
make test
```
This will set up your [virtualenv](https://pypi.python.org/pypi/virtualenv).
Now, in your terminal:
```bash
source .env/bin/activate
pip install git+https://github.com/tornadoweb/tornado.git
```
In the same terminal session, start the chatroom server:
```bash
python TestChatServer/py/chatroom.py
```
There's also a Go implementation (with the latest weekly) where you can:
```bash
cd TestChatServer/go
go run chatroom.go
```
#### Chatting
Now, start TestChat.app (just run the target in the Xcode project).
If you had it started already you can hit the refresh button to reconnect.
It should say "Connected!" on top.
To talk with the app, open up your browser to [http://localhost:9000](http://localhost:9000) and start chatting.
## WebSocket Server Implementation Recommendations
SocketRocket has been used with the following libraries:
- [Tornado](https://github.com/tornadoweb/tornado)
- Go's [WebSocket package](https://godoc.org/golang.org/x/net/websocket) or Gorilla's [version](http://www.gorillatoolkit.org/pkg/websocket).
- [Autobahn](http://autobahn.ws/testsuite/) (using its fuzzing client).
The Tornado one is dirt simple and works like a charm.
([IPython notebook](http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html) uses it too).
It's much easier to configure handlers and routes than in Autobahn/twisted.
## Contributing
Were glad youre interested in SocketRocket, and wed love to see where you take it.
Please read our [contributing guidelines](https://github.com/facebook/SocketRocket/blob/master/CONTRIBUTING.md) prior to submitting a Pull Request.
[build-status-svg]: https://img.shields.io/travis/facebook/SocketRocket/master.svg
[build-status-link]: https://travis-ci.org/facebook/SocketRocket/branches
[license-svg]: https://img.shields.io/badge/license-BSD-lightgrey.svg
[license-link]: https://github.com/facebook/SocketRocket/blob/master/LICENSE
[podspec-svg]: https://img.shields.io/cocoapods/v/SocketRocket.svg
[podspec-link]: https://cocoapods.org/pods/SocketRocket
[carthage-svg]: https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat
[carthage-link]: https://github.com/carthage/carthage
[platforms-svg]: http://img.shields.io/cocoapods/p/SocketRocket.svg?style=flat

View File

@ -1,246 +0,0 @@
SocketRocket Objective-C WebSocket Client (beta)
================================================
A conforming WebSocket (`RFC 6455 <https://tools.ietf.org/html/rfc6455>`_)
client library.
`Test results for SocketRocket here <http://square.github.io/SocketRocket/results/>`_.
You can compare to what `modern browsers look like here
<http://tavendo.com/autobahn/testsuite/report/clients/index.html>`_.
SocketRocket currently conforms to all ~300 of `Autobahn
<http://autobahn.ws/testsuite/>`_'s fuzzing tests (aside from
two UTF-8 ones where it is merely *non-strict*. tests 6.4.2 and 6.4.4)
Features/Design
---------------
- TLS (wss) support. It uses CFStream so we get this for *free*
- Uses NSStream/CFNetworking. Earlier implementations used ``dispatch_io``,
however, this proved to be make TLS nearly impossible. Also I wanted this to
work in iOS 4.x. (SocketRocket only supports 5.0 and above now)
- Uses ARC. It uses the 4.0 compatible subset (no weak refs).
- Seems to perform quite well
- Parallel architecture. Most of the work is done in background worker queues.
- Delegate-based. Had older versions that could use blocks too, but I felt it
didn't blend well with retain cycles and just objective C in general.
Changes
-------
v0.3.1-beta2 - 2013-01-12
`````````````````````````
- Stability fix for ``closeWithCode:reason:`` (Thanks @michaelpetrov!)
- Actually clean up the NSStreams and remove them from their runloops
- ``_SRRunLoopThread``'s ``main`` wasn't correctly wrapped with
``@autoreleasepool``
v0.3.1-beta1 - 2013-01-12
`````````````````````````
- Cleaned up GCD so OS_OBJECT_USE_OBJC_RETAIN_RELEASE is optional
- Removed deprecated ``dispatch_get_current_queue`` in favor of ``dispatch_queue_set_specific`` and ``dispatch_get_specific``
- Dropping support for iOS 4.0 (it may still work)
Installing (iOS)
----------------
There's a few options. Choose one, or just figure it out
- You can copy all the files in the SocketRocket group into your app.
- Include SocketRocket as a subproject and use libSocketRocket
If you do this, you must add -ObjC to your "other linker flags" option
- For OS X you will have to repackage make a .framework target. I will take
contributions. Message me if you are interested.
Depending on how you configure your project you may need to ``#import`` either
``<SocketRocket/SRWebSocket.h>`` or ``"SRWebSocket.h"``
Framework Dependencies
``````````````````````
Your .app must be linked against the following frameworks/dylibs
- libicucore.dylib
- CFNetwork.framework
- Security.framework
- Foundation.framework
Installing (OS X)
-----------------
SocketRocket now has (64-bit only) OS X support. ``SocketRocket.framework``
inside Xcode project is for OS X only. It should be identical in function aside
from the unicode validation. ICU isn't shipped with OS X which is what the
original implementation used for unicode validation. The workaround is much
more rudimentary and less robust.
1. Add SocketRocket.xcodeproj as either a subproject of your app or in your workspace.
2. Add ``SocketRocket.framework`` to the link libraries
3. If you don't have a "copy files" step for ``Framework``, create one
4. Add ``SocketRocket.framework`` to the "copy files" step.
API
---
The classes
``SRWebSocket``
```````````````
The Web Socket.
.. note:: ``SRWebSocket`` will retain itself between ``-(void)open`` and when it
closes, errors, or fails. This is similar to how ``NSURLConnection`` behaves.
(unlike ``NSURLConnection``, ``SRWebSocket`` won't retain the delegate)
What you need to know
.. code-block:: objective-c
@interface SRWebSocket : NSObject
// Make it with this
- (id)initWithURLRequest:(NSURLRequest *)request;
// Set this before opening
@property (nonatomic, assign) id <SRWebSocketDelegate> delegate;
- (void)open;
// Close it with this
- (void)close;
// Send a UTF8 String or Data
- (void)send:(id)data;
@end
``SRWebSocketDelegate``
```````````````````````
You implement this
.. code-block:: objective-c
@protocol SRWebSocketDelegate <NSObject>
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
@optional
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
@end
Known Issues/Server Todo's
--------------------------
- Needs auth delegates (like in NSURLConnection)
- Move the streams off the main runloop (most of the work is backgrounded uses
GCD, but I just haven't gotten around to moving it off the main loop since I
converted it from dispatch_io)
- Re-implement server. I removed an existing implementation as well because it
wasn't being used and I wasn't super happy with the interface. Will revisit
this.
- Separate framer and client logic. This will make it nicer when having a
server.
Testing
-------
Included are setup scripts for the python testing environment. It comes
packaged with vitualenv so all the dependencies are installed in userland.
To run the short test from the command line, run::
make test
To run all the tests, run::
make test_all
The short tests don't include the performance tests. (the test harness is
actually the bottleneck, not SocketRocket).
The first time this is run, it may take a while to install the dependencies. It
will be smooth sailing after that. After the test runs the makefile will open
the results page in your browser. If nothing comes up, you failed. Working on
making this interface a bit nicer.
To run from the app, choose the ``SocketRocket`` target and run the test action
(``cmd+u``). It runs the same thing, but makes it easier to debug. There is
some serious pre/post hooks in the Test action. You can edit it to customize
behavior.
.. note:: Xcode only up to version 4.4 is currently supported for the test
harness
TestChat Demo Application
-------------------------
SocketRocket includes a demo app, TestChat. It will "chat" with a listening
websocket on port 9900.
It's a simple project. Uses storyboard. Storyboard is sweet.
TestChat Server
```````````````
We've included a small server for the chat app. It has a simple function.
It will take a message and broadcast it to all other connected clients.
We have to get some dependencies. We also want to reuse the virtualenv we made
when we ran the tests. If you haven't run the tests yet, go into the
SocketRocket root directory and type::
make test
This will set up your `virtualenv <https://pypi.python.org/pypi/virtualenv>`_.
Now, in your terminal::
source .env/bin/activate
pip install git+https://github.com/tornadoweb/tornado.git
In the same terminal session, start the chatroom server::
python TestChatServer/py/chatroom.py
There's also a Go implementation (with the latest weekly) where you can::
cd TestChatServer/go
go run chatroom.go
Chatting
````````
Now, start TestChat.app (just run the target in the Xcode project). If you had
it started already you can hit the refresh button to reconnect. It should say
"Connected!" on top.
To talk with the app, open up your browser to `http://localhost:9000 <http://localhost:9000>`_ and
start chatting.
WebSocket Server Implementation Recommendations
-----------------------------------------------
SocketRocket has been used with the following libraries:
- `Tornado <https://github.com/tornadoweb/tornado>`_
- Go's `WebSocket package <https://godoc.org/golang.org/x/net/websocket>`_ or Gorilla's `version <http://www.gorillatoolkit.org/pkg/websocket>`_
- `Autobahn <http://tavendo.com/autobahn/testsuite.html>`_ (using its fuzzing
client)
The Tornado one is dirt simple and works like a charm. (`IPython notebook
<http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html>`_ uses it
too). It's much easier to configure handlers and routes than in
Autobahn/twisted.
As far as Go's goes, it works in my limited testing. I much prefer go's
concurrency model as well. Try it! You may like it.
It could use some more control over things such as pings, etc., but I
am sure it will come in time.
Autobahn is a great test suite. The Python server code is good, and conforms
well (obviously). However for me, twisted would be a deal-breaker for writing
something new. I find it a bit too complex and heavy for a simple service. If
you are already using twisted though, Autobahn is probably for you.
Contributing
------------
Were glad youre interested in SocketRocket, and wed love to see where you take it. Please read our `contributing guidelines <https://github.com/facebook/SocketRocket/blob/master/Contributing.md>`_ prior to submitting a Pull Request.

View File

@ -1,15 +1,15 @@
Pod::Spec.new do |s|
s.name = "SocketRocket"
s.name = 'SocketRocket'
s.version = '0.5.1'
s.summary = 'A conforming WebSocket (RFC 6455) client library.'
s.summary = 'A conforming WebSocket (RFC 6455) client library for iOS, macOS and tvOS.'
s.homepage = 'https://github.com/facebook/SocketRocket'
s.authors = 'Square'
s.authors = { 'Nikita Lutsenko' => 'nlutsenko@me.com', 'Dan Federman' => 'federman@squareup.com', 'Mike Lewis' => 'mikelikespie@gmail.com' }
s.license = 'BSD'
s.source = { :git => 'https://github.com/facebook/SocketRocket.git', :tag => s.version.to_s }
s.requires_arc = true
s.source_files = 'SocketRocket/**/*.{h,m}'
s.public_header_files = 'SocketRocket/*.h'
s.public_header_files = 'SocketRocket/*.h'
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
@ -18,6 +18,5 @@ Pod::Spec.new do |s|
s.ios.frameworks = 'CFNetwork', 'Security'
s.osx.frameworks = 'CoreServices', 'Security'
s.tvos.frameworks = 'CFNetwork', 'Security'
s.libraries = "icucore"
s.libraries = 'icucore'
end

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS-Dynamic"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS-Dynamic"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS-Dynamic"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
LastUpgradeVersion = "0730"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
@ -14,12 +14,26 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BlueprintIdentifier = "81D6475F1D2CA78800690609"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
@ -27,8 +41,63 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "PIDFILE=$TMPDIR/sr_test_server.pid&#10;&#10;if [ -r $PIDFILE ]; then&#10;EXISTING_PID=`cat $PIDFILE`&#10;echo &quot;Killing Dangling Autobahn Server PID:&quot; $EXISTING_PID&#10;kill $EXISTING_PID || true&#10;rm $PIDFILE&#10;fi&#10;&#10;pushd $PROJECT_DIR&#10;&#10;source .env/bin/activate&#10;nohup ./TestSupport/run_test_server.sh &amp;&#10;&#10;echo $! &gt; $PIDFILE&#10;&#10;popd&#10;">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<PostActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "PIDFILE=$TMPDIR/sr_test_server.pid&#10;&#10;if [ -r $PIDFILE ]; then&#10;EXISTING_PID=`cat $PIDFILE`&#10;echo &quot;Killing SR TestServer PID:&quot; $EXISTING_PID&#10;kill $EXISTING_PID&#10;rm $PIDFILE&#10;fi&#10;">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PostActions>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81D6475F1D2CA78800690609"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
@ -45,7 +114,7 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BlueprintIdentifier = "81D6475F1D2CA78800690609"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
@ -63,7 +132,7 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BlueprintIdentifier = "81D6475F1D2CA78800690609"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">

View File

@ -16,7 +16,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "F668C87F153E91210044DBAC"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-OSX"
BlueprintName = "SocketRocket-macOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
@ -47,7 +47,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "F668C87F153E91210044DBAC"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-OSX"
BlueprintName = "SocketRocket-macOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
@ -65,7 +65,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "F668C87F153E91210044DBAC"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-OSX"
BlueprintName = "SocketRocket-macOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0730"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@ -34,7 +34,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "2D4227611BB4358C000C1A6C"
BuildableName = "SocketRocket.framework"
BlueprintName = "SocketRocket-iOS"
BlueprintName = "SocketRocket-iOS-Dynamic"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>

View File

@ -1,169 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6B2082C1450F597009315AF"
BuildableName = "libSocketRocket.a"
BlueprintName = "SocketRocket"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "NO">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "PIDFILE=$TMPDIR/srtharness.pid&#10;if [ -r $PIDFILE ]; then&#10; EXISTING_PID=`cat $PIDFILE`&#10; echo &quot;Killing Dangling SRTextharneess PID:&quot; $EXISTING_PID&#10; kill $EXISTING_PID&#10; rm $PIDFILE&#10;fi&#10;&#10;pushd $PROJECT_DIR&#10;&#10;export MACOSX_DEPLOYMENT_TARGET=&#10;&#10;bash TestSupport/ensure_virtualenv.sh $PROJECT_DIR/.env&#10;source .env/bin/activate&#10;&#10;rm -rf &quot;$PROJECT_DIR/reports/clients/&quot;&#10;nohup sr-testharness -i &apos;&apos; -c &apos;*&apos; &amp;&#10;&#10;#nohup sr-testharness -k hello_test_harness -i &apos;&apos; -c &apos;*&apos; &amp;&#10;&#10;echo $! &gt; $PIDFILE&#10;&#10;popd"
shellToInvoke = "/bin/bash">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<PostActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "PIDFILE=$TMPDIR/srtharness.pid&#10;if [ -r $PIDFILE ]; then&#10; EXISTING_PID=`cat $PIDFILE`&#10; echo &quot;Killing SRTestharness PID:&quot; $EXISTING_PID&#10; kill $EXISTING_PID&#10; rm $PIDFILE&#10;fi&#10;&#10;&#10;open $PROJECT_DIR/pages/results/index.html&#10;"
shellToInvoke = "/bin/bash">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PostActions>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6B2082C1450F597009315AF"
BuildableName = "libSocketRocket.a"
BlueprintName = "SocketRocket"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
<EnvironmentVariables>
<EnvironmentVariable
key = "SR_TEST_URL"
value = "ws://localhost:9001"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6B2082C1450F597009315AF"
BuildableName = "libSocketRocket.a"
BlueprintName = "SocketRocket"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "OBJC_PRINT_EXCEPTIONS"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "MallocGuardEdges"
value = ""
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "MallocScribble"
value = ""
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0730"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@ -15,8 +15,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</BuildActionEntry>
@ -33,8 +33,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</TestableReference>
@ -56,8 +56,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6BDA801145900D200FE3253"
BuildableName = "SRWebSocketTests.xctest"
BlueprintName = "SRWebSocketTests"
BuildableName = "SocketRocketTests-iOS.xctest"
BlueprintName = "SocketRocketTests-iOS"
ReferencedContainer = "container:SocketRocket.xcodeproj">
</BuildableReference>
</MacroExpansion>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0730"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"

View File

@ -9,10 +9,12 @@
#import <Foundation/Foundation.h>
#import "SRWebSocket.h"
#import <SocketRocket/SRWebSocket.h>
NS_ASSUME_NONNULL_BEGIN
#if OBJC_BOOL_IS_BOOL
struct SRDelegateAvailableMethods {
BOOL didReceiveMessage : 1;
BOOL didReceiveMessageWithString : 1;
@ -20,9 +22,27 @@ struct SRDelegateAvailableMethods {
BOOL didOpen : 1;
BOOL didFailWithError : 1;
BOOL didCloseWithCode : 1;
BOOL didReceivePing : 1;
BOOL didReceivePong : 1;
BOOL shouldConvertTextFrameToString : 1;
};
#else
struct SRDelegateAvailableMethods {
BOOL didReceiveMessage;
BOOL didReceiveMessageWithString;
BOOL didReceiveMessageWithData;
BOOL didOpen;
BOOL didFailWithError;
BOOL didCloseWithCode;
BOOL didReceivePing;
BOOL didReceivePong;
BOOL shouldConvertTextFrameToString;
};
#endif
typedef struct SRDelegateAvailableMethods SRDelegateAvailableMethods;
typedef void(^SRDelegateBlock)(id<SRWebSocketDelegate> _Nullable delegate, SRDelegateAvailableMethods availableMethods);

View File

@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) dispatch_queue_t accessQueue;
@property (atomic, readwrite) SRDelegateAvailableMethods availableDelegateMethods;
@property (atomic, assign, readwrite) SRDelegateAvailableMethods availableDelegateMethods;
@end
@ -56,6 +56,7 @@ NS_ASSUME_NONNULL_BEGIN
.didOpen = [delegate respondsToSelector:@selector(webSocketDidOpen:)],
.didFailWithError = [delegate respondsToSelector:@selector(webSocket:didFailWithError:)],
.didCloseWithCode = [delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)],
.didReceivePing = [delegate respondsToSelector:@selector(webSocket:didReceivePingWithData:)],
.didReceivePong = [delegate respondsToSelector:@selector(webSocket:didReceivePong:)],
.shouldConvertTextFrameToString = [delegate respondsToSelector:@selector(webSocketShouldConvertTextFrameToString:)]
};

View File

@ -0,0 +1,13 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <SocketRocket/NSRunLoop+SRWebSocket.h>
// Empty function that force links the object file for the category.
extern void import_NSRunLoop_SRWebSocket(void);

View File

@ -0,0 +1,13 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <SocketRocket/NSURLRequest+SRWebSocket.h>
// Empty function that force links the object file for the category.
extern void import_NSURLRequest_SRWebSocket(void);

View File

@ -8,11 +8,12 @@
//
#import "SRProxyConnect.h"
#import "SRError.h"
#import "NSRunLoop+SRWebSocket.h"
#import "SRURLUtilities.h"
static inline void SRProxyFastLog(NSString *format, ...);
#import "NSRunLoop+SRWebSocket.h"
#import "SRConstants.h"
#import "SRError.h"
#import "SRLog.h"
#import "SRURLUtilities.h"
@interface SRProxyConnect() <NSStreamDelegate>
@ -39,7 +40,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
BOOL _connectionRequiresSSL;
NSMutableArray<NSData *> *_inputQueue;
NSOperationQueue * _writeQueue;
dispatch_queue_t _writeQueue;
}
///--------------------------------------
@ -54,12 +55,26 @@ static inline void SRProxyFastLog(NSString *format, ...);
_url = url;
_connectionRequiresSSL = SRURLRequiresSSL(url);
_writeQueue = [[NSOperationQueue alloc] init];
_writeQueue = dispatch_queue_create("com.facebook.socketrocket.proxyconnect.write", DISPATCH_QUEUE_SERIAL);
_inputQueue = [NSMutableArray arrayWithCapacity:2];
return self;
}
- (void)dealloc
{
// If we get deallocated before the socket open finishes - we need to cleanup everything.
[self.inputStream removeFromRunLoop:[NSRunLoop SR_networkRunLoop] forMode:NSDefaultRunLoopMode];
self.inputStream.delegate = nil;
[self.inputStream close];
self.inputStream = nil;
self.outputStream.delegate = nil;
[self.outputStream close];
self.outputStream = nil;
}
///--------------------------------------
#pragma mark - Open
///--------------------------------------
@ -76,11 +91,11 @@ static inline void SRProxyFastLog(NSString *format, ...);
- (void)_didConnect
{
SRProxyFastLog(@"_didConnect, return streams");
SRDebugLog(@"_didConnect, return streams");
if (_connectionRequiresSSL) {
if (_httpProxyHost) {
// Must set the real peer name before turning on SSL
SRProxyFastLog(@"proxy set peer name to real host %@", self.url.host);
SRDebugLog(@"proxy set peer name to real host %@", self.url.host);
[self.outputStream setProperty:self.url.host forKey:@"_kCFStreamPropertySocketPeerName"];
}
}
@ -104,7 +119,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
- (void)_failWithError:(NSError *)error
{
SRProxyFastLog(@"_failWithError, return error");
SRDebugLog(@"_failWithError, return error");
if (!error) {
error = SRHTTPErrorWithCodeDescription(500, 2132,@"Proxy Error");
}
@ -113,6 +128,10 @@ static inline void SRProxyFastLog(NSString *format, ...);
CFRelease(_receivedHTTPHeaders);
_receivedHTTPHeaders = NULL;
}
self.inputStream.delegate = nil;
self.outputStream.delegate = nil;
[self.inputStream removeFromRunLoop:[NSRunLoop SR_networkRunLoop]
forMode:NSDefaultRunLoopMode];
[self.inputStream close];
@ -125,7 +144,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
// get proxy setting from device setting
- (void)_configureProxy
{
SRProxyFastLog(@"configureProxy");
SRDebugLog(@"configureProxy");
NSDictionary *proxySettings = CFBridgingRelease(CFNetworkCopySystemProxySettings());
// CFNetworkCopyProxiesForURL doesn't understand ws:// or wss://
@ -138,7 +157,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
NSArray *proxies = CFBridgingRelease(CFNetworkCopyProxiesForURL((__bridge CFURLRef)httpURL, (__bridge CFDictionaryRef)proxySettings));
if (proxies.count == 0) {
SRProxyFastLog(@"configureProxy no proxies");
SRDebugLog(@"configureProxy no proxies");
[self _openConnection];
return; // no proxy
}
@ -147,14 +166,14 @@ static inline void SRProxyFastLog(NSString *format, ...);
if ([proxyType isEqualToString:(NSString *)kCFProxyTypeAutoConfigurationURL]) {
NSURL *pacURL = settings[(NSString *)kCFProxyAutoConfigurationURLKey];
if (pacURL) {
[self _fetchPAC:pacURL];
[self _fetchPAC:pacURL withProxySettings:proxySettings];
return;
}
}
if ([proxyType isEqualToString:(__bridge NSString *)kCFProxyTypeAutoConfigurationJavaScript]) {
NSString *script = settings[(__bridge NSString *)kCFProxyAutoConfigurationJavaScriptKey];
if (script) {
[self _runPACScript:script];
[self _runPACScript:script withProxySettings:proxySettings];
return;
}
}
@ -169,8 +188,9 @@ static inline void SRProxyFastLog(NSString *format, ...);
[proxyType isEqualToString:(NSString *)kCFProxyTypeHTTPS]) {
_httpProxyHost = settings[(NSString *)kCFProxyHostNameKey];
NSNumber *portValue = settings[(NSString *)kCFProxyPortNumberKey];
if (portValue)
if (portValue) {
_httpProxyPort = [portValue intValue];
}
}
if ([proxyType isEqualToString:(NSString *)kCFProxyTypeSOCKS]) {
_socksProxyHost = settings[(NSString *)kCFProxyHostNameKey];
@ -181,17 +201,17 @@ static inline void SRProxyFastLog(NSString *format, ...);
_socksProxyPassword = settings[(NSString *)kCFProxyPasswordKey];
}
if (_httpProxyHost) {
SRProxyFastLog(@"Using http proxy %@:%u", _httpProxyHost, _httpProxyPort);
SRDebugLog(@"Using http proxy %@:%u", _httpProxyHost, _httpProxyPort);
} else if (_socksProxyHost) {
SRProxyFastLog(@"Using socks proxy %@:%u", _socksProxyHost, _socksProxyPort);
SRDebugLog(@"Using socks proxy %@:%u", _socksProxyHost, _socksProxyPort);
} else {
SRProxyFastLog(@"configureProxy no proxies");
SRDebugLog(@"configureProxy no proxies");
}
}
- (void)_fetchPAC:(NSURL *)PACurl
- (void)_fetchPAC:(NSURL *)PACurl withProxySettings:(NSDictionary *)proxySettings
{
SRProxyFastLog(@"SRWebSocket fetchPAC:%@", PACurl);
SRDebugLog(@"SRWebSocket fetchPAC:%@", PACurl);
if ([PACurl isFileURL]) {
NSError *error = nil;
@ -202,7 +222,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
if (error) {
[self _openConnection];
} else {
[self _runPACScript:script];
[self _runPACScript:script withProxySettings:proxySettings];
}
return;
}
@ -214,36 +234,32 @@ static inline void SRProxyFastLog(NSString *format, ...);
[self _openConnection];
return;
}
__weak typeof(self) weakSelf = self;
__weak typeof(self) wself = self;
NSURLRequest *request = [NSURLRequest requestWithURL:PACurl];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSString* script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[weakSelf _runPACScript:script];
} else {
[weakSelf _openConnection];
}
}];
[task resume];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
__strong typeof(wself) sself = wself;
if (!error) {
NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[sself _runPACScript:script withProxySettings:proxySettings];
} else {
[sself _openConnection];
}
}] resume];
}
- (void)_runPACScript:(NSString *)script
- (void)_runPACScript:(NSString *)script withProxySettings:(NSDictionary *)proxySettings
{
if (!script) {
[self _openConnection];
return;
}
SRProxyFastLog(@"runPACScript");
SRDebugLog(@"runPACScript");
// From: http://developer.apple.com/samplecode/CFProxySupportTool/listing1.html
// Work around <rdar://problem/5530166>. This dummy call to
// CFNetworkCopyProxiesForURL initialise some state within CFNetwork
// that is required by CFNetworkCopyProxiesForAutoConfigurationScript.
NSDictionary *empty = nil;
CFBridgingRelease(CFNetworkCopyProxiesForURL((__bridge CFURLRef)_url, (__bridge CFDictionaryRef)empty));
CFBridgingRelease(CFNetworkCopyProxiesForURL((__bridge CFURLRef)_url, (__bridge CFDictionaryRef)proxySettings));
// Obtain the list of proxies by running the autoconfiguration script
CFErrorRef err = NULL;
@ -293,14 +309,14 @@ static inline void SRProxyFastLog(NSString *format, ...);
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
SRProxyFastLog(@"ProxyConnect connect stream to %@:%u", host, port);
SRDebugLog(@"ProxyConnect connect stream to %@:%u", host, port);
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);
self.outputStream = CFBridgingRelease(writeStream);
self.inputStream = CFBridgingRelease(readStream);
if (_socksProxyHost) {
SRProxyFastLog(@"ProxyConnect set sock property stream to %@:%u user %@ password %@", _socksProxyHost, _socksProxyPort, _socksProxyUsername, _socksProxyPassword);
SRDebugLog(@"ProxyConnect set sock property stream to %@:%u user %@ password %@", _socksProxyHost, _socksProxyPort, _socksProxyUsername, _socksProxyPassword);
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:4];
settings[NSStreamSOCKSProxyHostKey] = _socksProxyHost;
if (_socksProxyPort) {
@ -321,7 +337,7 @@ static inline void SRProxyFastLog(NSString *format, ...);
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;
{
SRProxyFastLog(@"stream handleEvent %u", eventCode);
SRDebugLog(@"stream handleEvent %u", eventCode);
switch (eventCode) {
case NSStreamEventOpenCompleted: {
if (aStream == self.inputStream) {
@ -343,15 +359,16 @@ static inline void SRProxyFastLog(NSString *format, ...);
[self _processInputStream];
}
} break;
default:
SRProxyFastLog(@"(default) %@", aStream);
case NSStreamEventHasSpaceAvailable:
case NSStreamEventNone:
SRDebugLog(@"(default) %@", aStream);
break;
}
}
- (void)_proxyDidConnect
{
SRProxyFastLog(@"Proxy Connected");
SRDebugLog(@"Proxy Connected");
uint32_t port = _url.port.unsignedIntValue;
if (port == 0) {
port = (_connectionRequiresSSL ? 443 : 80);
@ -360,19 +377,17 @@ static inline void SRProxyFastLog(NSString *format, ...);
NSString *connectRequestStr = [NSString stringWithFormat:@"CONNECT %@:%u HTTP/1.1\r\nHost: %@\r\nConnection: keep-alive\r\nProxy-Connection: keep-alive\r\n\r\n", _url.host, port, _url.host];
NSData *message = [connectRequestStr dataUsingEncoding:NSUTF8StringEncoding];
SRProxyFastLog(@"Proxy sending %@", connectRequestStr);
SRDebugLog(@"Proxy sending %@", connectRequestStr);
[self _writeData:message];
}
static size_t const SRProxyConnectBufferMaxSize = 4096;
///handles the incoming bytes and sending them to the proper processing method
- (void)_processInputStream
{
NSMutableData *buf = [NSMutableData dataWithCapacity:SRProxyConnectBufferMaxSize];
NSMutableData *buf = [NSMutableData dataWithCapacity:SRDefaultBufferSize()];
uint8_t *buffer = buf.mutableBytes;
NSInteger length = [_inputStream read:buffer maxLength:SRProxyConnectBufferMaxSize];
NSInteger length = [_inputStream read:buffer maxLength:SRDefaultBufferSize()];
if (length <= 0) {
return;
@ -391,13 +406,17 @@ static size_t const SRProxyConnectBufferMaxSize = 4096;
- (void)_dequeueInput
{
while (_inputQueue.count > 0) {
NSData * data = _inputQueue[0];
[self _proxyProcessHTTPResponseWithData:data];
NSData *data = _inputQueue.firstObject;
[_inputQueue removeObjectAtIndex:0];
// No need to process any data further, we got the full header data.
if ([self _proxyProcessHTTPResponseWithData:data]) {
break;
}
}
}
//handle checking the proxy connection status
- (void)_proxyProcessHTTPResponseWithData:(NSData *)data
- (BOOL)_proxyProcessHTTPResponseWithData:(NSData *)data
{
if (_receivedHTTPHeaders == NULL) {
_receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO);
@ -405,9 +424,12 @@ static size_t const SRProxyConnectBufferMaxSize = 4096;
CFHTTPMessageAppendBytes(_receivedHTTPHeaders, (const UInt8 *)data.bytes, data.length);
if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) {
SRProxyFastLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
SRDebugLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
[self _proxyHTTPHeadersDidFinish];
return YES;
}
return NO;
}
- (void)_proxyHTTPHeadersDidFinish
@ -415,14 +437,14 @@ static size_t const SRProxyConnectBufferMaxSize = 4096;
NSInteger responseCode = CFHTTPMessageGetResponseStatusCode(_receivedHTTPHeaders);
if (responseCode >= 299) {
SRProxyFastLog(@"Connect to Proxy Request failed with response code %d", responseCode);
SRDebugLog(@"Connect to Proxy Request failed with response code %d", responseCode);
NSError *error = SRHTTPErrorWithCodeDescription(responseCode, 2132,
[NSString stringWithFormat:@"Received bad response code from proxy server: %d.",
(int)responseCode]);
[self _failWithError:error];
return;
}
SRProxyFastLog(@"proxy connect return %d, call socket connect", responseCode);
SRDebugLog(@"proxy connect return %d, call socket connect", responseCode);
[self _didConnect];
}
@ -431,13 +453,14 @@ static NSTimeInterval const SRProxyConnectWriteTimeout = 5.0;
- (void)_writeData:(NSData *)data
{
const uint8_t * bytes = data.bytes;
__block NSInteger timeout = SRProxyConnectWriteTimeout * 1000000; // wait timeout before giving up
__block NSInteger timeout = (NSInteger)(SRProxyConnectWriteTimeout * 1000000); // wait timeout before giving up
__weak typeof(self) wself = self;
[_writeQueue addOperationWithBlock:^() {
if (!wself) {
dispatch_async(_writeQueue, ^{
__strong typeof(wself) sself = self;
if (!sself) {
return;
}
NSOutputStream *outStream = wself.outputStream;
NSOutputStream *outStream = sself.outputStream;
if (!outStream) {
return;
}
@ -446,27 +469,13 @@ static NSTimeInterval const SRProxyConnectWriteTimeout = 5.0;
timeout -= 100;
if (timeout < 0) {
NSError *error = SRHTTPErrorWithCodeDescription(408, 2132, @"Proxy timeout");
[self _failWithError:error];
[sself _failWithError:error];
} else if (outStream.streamError != nil) {
[self _failWithError:outStream.streamError];
[sself _failWithError:outStream.streamError];
}
}
[outStream write:bytes maxLength:data.length];
}];
});
}
@end
//#define SRPROXY_ENABLE_LOG
static inline void SRProxyFastLog(NSString *format, ...) {
#ifdef SRPROXY_ENABLE_LOG
__block va_list arg_list;
va_start (arg_list, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arg_list];
va_end(arg_list);
NSLog(@"[Proxy] %@", formattedString);
#endif
}

View File

@ -12,21 +12,15 @@
#import "SRRunLoopThread.h"
@interface SRRunLoopThread ()
{
dispatch_group_t _waitGroup;
}
@property (nonatomic, strong, readwrite) NSRunLoop *runLoop;
@end
@implementation SRRunLoopThread {
dispatch_group_t _waitGroup;
}
- (void)dealloc
{
#if !OS_OBJECT_USE_OBJC_RETAIN_RELEASE
dispatch_release(_waitGroup);
#endif
}
@implementation SRRunLoopThread
+ (instancetype)sharedThread
{

View File

@ -0,0 +1,26 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, SROpCode)
{
SROpCodeTextFrame = 0x1,
SROpCodeBinaryFrame = 0x2,
// 3-7 reserved.
SROpCodeConnectionClose = 0x8,
SROpCodePing = 0x9,
SROpCodePong = 0xA,
// B-F reserved.
};
/**
Default buffer size that is used for reading/writing to streams.
*/
extern size_t SRDefaultBufferSize(void);

View File

@ -0,0 +1,19 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRConstants.h"
size_t SRDefaultBufferSize(void) {
static size_t size;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
size = getpagesize();
});
return size;
}

View File

@ -0,0 +1,27 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
#import <SocketRocket/SRSecurityPolicy.h>
NS_ASSUME_NONNULL_BEGIN
/**
* NOTE: While publicly, SocketRocket does not support configuring the security policy with pinned certificates,
* it is still possible to manually construct a security policy of this class. If you do this, note that you may
* be open to MitM attacks, and we will not support any issues you may have. Dive at your own risk.
*/
@interface SRPinningSecurityPolicy : SRSecurityPolicy
- (instancetype)initWithCertificates:(NSArray *)pinnedCertificates;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,73 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRPinningSecurityPolicy.h"
#import <Foundation/Foundation.h>
#import "SRLog.h"
NS_ASSUME_NONNULL_BEGIN
@interface SRPinningSecurityPolicy ()
@property (nonatomic, copy, readonly) NSArray *pinnedCertificates;
@end
@implementation SRPinningSecurityPolicy
- (instancetype)initWithCertificates:(NSArray *)pinnedCertificates
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
// Do not validate certificate chain since we're pinning to specific certificates.
self = [super initWithCertificateChainValidationEnabled:NO];
#pragma clang diagnostic pop
if (!self) { return self; }
if (pinnedCertificates.count == 0) {
@throw [NSException exceptionWithName:@"Creating security policy failed."
reason:@"Must specify at least one certificate when creating a pinning policy."
userInfo:nil];
}
_pinnedCertificates = [pinnedCertificates copy];
return self;
}
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain
{
SRDebugLog(@"Pinned cert count: %d", self.pinnedCertificates.count);
NSUInteger requiredCertCount = self.pinnedCertificates.count;
NSUInteger validatedCertCount = 0;
CFIndex serverCertCount = SecTrustGetCertificateCount(serverTrust);
for (CFIndex i = 0; i < serverCertCount; i++) {
SecCertificateRef cert = SecTrustGetCertificateAtIndex(serverTrust, i);
NSData *data = CFBridgingRelease(SecCertificateCopyData(cert));
for (id ref in self.pinnedCertificates) {
SecCertificateRef trustedCert = (__bridge SecCertificateRef)ref;
// TODO: (nlutsenko) Add caching, so we don't copy the data for every pinned cert all the time.
NSData *trustedCertData = CFBridgingRelease(SecCertificateCopyData(trustedCert));
if ([trustedCertData isEqualToData:data]) {
validatedCertCount++;
break;
}
}
}
return (requiredCertCount == validatedCertCount);
}
@end
NS_ASSUME_NONNULL_END

View File

@ -8,6 +8,7 @@
//
#import "SRError.h"
#import "SRWebSocket.h"
NS_ASSUME_NONNULL_BEGIN

View File

@ -0,0 +1,20 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
extern CFHTTPMessageRef SRHTTPConnectMessageCreate(NSURLRequest *request,
NSString *securityKey,
uint8_t webSocketProtocolVersion,
NSArray<NSHTTPCookie *> *_Nullable cookies,
NSArray<NSString *> *_Nullable requestedProtocols);
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,79 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRHTTPConnectMessage.h"
#import "SRURLUtilities.h"
NS_ASSUME_NONNULL_BEGIN
static NSString *_SRHTTPConnectMessageHost(NSURL *url)
{
NSString *host = url.host;
if (url.port) {
host = [host stringByAppendingFormat:@":%@", url.port];
}
return host;
}
CFHTTPMessageRef SRHTTPConnectMessageCreate(NSURLRequest *request,
NSString *securityKey,
uint8_t webSocketProtocolVersion,
NSArray<NSHTTPCookie *> *_Nullable cookies,
NSArray<NSString *> *_Nullable requestedProtocols)
{
NSURL *url = request.URL;
CFHTTPMessageRef message = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)url, kCFHTTPVersion1_1);
// Set host first so it defaults
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Host"), (__bridge CFStringRef)_SRHTTPConnectMessageHost(url));
NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16];
int result = SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.mutableBytes);
if (result != 0) {
//TODO: (nlutsenko) Check if there was an error.
}
// Apply cookies if any have been provided
if (cookies) {
NSDictionary<NSString *, NSString *> *messageCookies = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[messageCookies enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
if (key.length && obj.length) {
CFHTTPMessageSetHeaderFieldValue(message, (__bridge CFStringRef)key, (__bridge CFStringRef)obj);
}
}];
}
// set header for http basic auth
NSString *basicAuthorizationString = SRBasicAuthorizationHeaderFromURL(url);
if (basicAuthorizationString) {
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Authorization"), (__bridge CFStringRef)basicAuthorizationString);
}
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Upgrade"), CFSTR("websocket"));
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Connection"), CFSTR("Upgrade"));
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Sec-WebSocket-Key"), (__bridge CFStringRef)securityKey);
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Sec-WebSocket-Version"), (__bridge CFStringRef)@(webSocketProtocolVersion).stringValue);
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Origin"), (__bridge CFStringRef)SRURLOrigin(url));
if (requestedProtocols.count) {
CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Sec-WebSocket-Protocol"),
(__bridge CFStringRef)[requestedProtocols componentsJoinedByString:@", "]);
}
[request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
CFHTTPMessageSetHeaderFieldValue(message, (__bridge CFStringRef)key, (__bridge CFStringRef)obj);
}];
return message;
}
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,20 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// Uncomment this line to enable debug logging
//#define SR_DEBUG_LOG_ENABLED
extern void SRErrorLog(NSString *format, ...);
extern void SRDebugLog(NSString *format, ...);
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,33 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRLog.h"
NS_ASSUME_NONNULL_BEGIN
extern void SRErrorLog(NSString *format, ...)
{
__block va_list arg_list;
va_start (arg_list, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arg_list];
va_end(arg_list);
NSLog(@"[SocketRocket] %@", formattedString);
}
extern void SRDebugLog(NSString *format, ...)
{
#ifdef SR_DEBUG_LOG_ENABLED
SRErrorLog(tag, format);
#endif
}
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,22 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef __attribute__((capability("mutex"))) pthread_mutex_t *SRMutex;
extern SRMutex SRMutexInitRecursive(void);
extern void SRMutexDestroy(SRMutex mutex);
extern void SRMutexLock(SRMutex mutex) __attribute__((acquire_capability(mutex)));
extern void SRMutexUnlock(SRMutex mutex) __attribute__((release_capability(mutex)));
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,47 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRMutex.h"
#import <pthread/pthread.h>
NS_ASSUME_NONNULL_BEGIN
SRMutex SRMutexInitRecursive(void)
{
pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
pthread_mutexattr_t attributes;
pthread_mutexattr_init(&attributes);
pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(mutex, &attributes);
pthread_mutexattr_destroy(&attributes);
return mutex;
}
void SRMutexDestroy(SRMutex mutex)
{
pthread_mutex_destroy(mutex);
free(mutex);
}
__attribute__((no_thread_safety_analysis))
void SRMutexLock(SRMutex mutex)
{
pthread_mutex_lock(mutex);
}
__attribute__((no_thread_safety_analysis))
void SRMutexUnlock(SRMutex mutex)
{
pthread_mutex_unlock(mutex);
}
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,16 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
extern NSData *SRRandomData(NSUInteger length);
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,26 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRRandom.h"
#import <Security/SecRandom.h>
NS_ASSUME_NONNULL_BEGIN
NSData *SRRandomData(NSUInteger length)
{
NSMutableData *data = [NSMutableData dataWithLength:length];
int result = SecRandomCopyBytes(kSecRandomDefault, data.length, data.mutableBytes);
if (result != 0) {
[NSException raise:NSInternalInconsistencyException format:@"Failed to generate random bytes with OSStatus: %d", result];
}
return data;
}
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,19 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <Foundation/Foundation.h>
/**
Unmask bytes using XOR via SIMD.
@param bytes The bytes to unmask.
@param length The number of bytes to unmask.
@param maskKey The mask to XOR with MUST be of length sizeof(uint32_t).
*/
void SRMaskBytesSIMD(uint8_t *bytes, size_t length, uint8_t *maskKey);

View File

@ -0,0 +1,73 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRSIMDHelpers.h"
typedef uint8_t uint8x32_t __attribute__((vector_size(32)));
static void SRMaskBytesManual(uint8_t *bytes, size_t length, uint8_t *maskKey) {
for (size_t i = 0; i < length; i++) {
bytes[i] = bytes[i] ^ maskKey[i % sizeof(uint32_t)];
}
}
/**
Right-shift the elements of a vector, circularly.
@param vector The vector to circular shift.
@param by The number of elements to shift by.
@return A shifted vector.
*/
static uint8x32_t SRShiftVector(uint8x32_t vector, size_t by) {
uint8x32_t vectorCopy = vector;
by = by % _Alignof(uint8x32_t);
uint8_t *vectorPointer = (uint8_t *)&vector;
uint8_t *vectorCopyPointer = (uint8_t *)&vectorCopy;
memmove(vectorPointer + by, vectorPointer, sizeof(vector) - by);
memcpy(vectorPointer, vectorCopyPointer + (sizeof(vector) - by), by);
return vector;
}
void SRMaskBytesSIMD(uint8_t *bytes, size_t length, uint8_t *maskKey) {
size_t alignmentBytes = _Alignof(uint8x32_t) - ((uintptr_t)bytes % _Alignof(uint8x32_t));
if (alignmentBytes == _Alignof(uint8x32_t)) {
alignmentBytes = 0;
}
// If the number of bytes that can be processed after aligning is
// less than the number of bytes we can put into a vector,
// then there's no work to do with SIMD, just call the manual version.
if (alignmentBytes > length || (length - alignmentBytes) < sizeof(uint8x32_t)) {
SRMaskBytesManual(bytes, length, maskKey);
return;
}
size_t vectorLength = (length - alignmentBytes) / sizeof(uint8x32_t);
size_t manualStartOffset = alignmentBytes + (vectorLength * sizeof(uint8x32_t));
size_t manualLength = length - manualStartOffset;
uint8x32_t *vector = (uint8x32_t *)(bytes + alignmentBytes);
uint8x32_t maskVector = { };
memset_pattern4(&maskVector, maskKey, sizeof(uint8x32_t));
maskVector = SRShiftVector(maskVector, alignmentBytes);
SRMaskBytesManual(bytes, alignmentBytes, maskKey);
for (size_t vectorIndex = 0; vectorIndex < vectorLength; vectorIndex++) {
vector[vectorIndex] = vector[vectorIndex] ^ maskVector;
}
// Use the shifted mask for the final manual part.
SRMaskBytesManual(bytes + manualStartOffset, manualLength, (uint8_t *) &maskVector);
}

View File

@ -20,4 +20,7 @@ extern BOOL SRURLRequiresSSL(NSURL *url);
// Extracts `user` and `password` from url (if available) into `Basic base64(user:password)`.
extern NSString *_Nullable SRBasicAuthorizationHeaderFromURL(NSURL *url);
// Returns a valid value for `NSStreamNetworkServiceType` or `nil`.
extern NSString *_Nullable SRStreamNetworkServiceTypeFromURLRequest(NSURLRequest *request);
NS_ASSUME_NONNULL_END

View File

@ -9,6 +9,8 @@
#import "SRURLUtilities.h"
#import "SRHash.h"
NS_ASSUME_NONNULL_BEGIN
NSString *SRURLOrigin(NSURL *url)
@ -42,16 +44,34 @@ extern BOOL SRURLRequiresSSL(NSURL *url)
extern NSString *_Nullable SRBasicAuthorizationHeaderFromURL(NSURL *url)
{
NSData *data = [[NSString stringWithFormat:@"%@:%@", url.user, url.password] dataUsingEncoding:NSUTF8StringEncoding];
NSString *userAndPasswordBase64Encoded;
if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
userAndPasswordBase64Encoded = [data base64EncodedStringWithOptions:0];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
userAndPasswordBase64Encoded = [data base64Encoding];
#pragma clang diagnostic pop
return [NSString stringWithFormat:@"Basic %@", SRBase64EncodedStringFromData(data)];
}
extern NSString *_Nullable SRStreamNetworkServiceTypeFromURLRequest(NSURLRequest *request)
{
NSString *networkServiceType = nil;
switch (request.networkServiceType) {
case NSURLNetworkServiceTypeDefault:
break;
case NSURLNetworkServiceTypeVoIP:
networkServiceType = NSStreamNetworkServiceTypeVoIP;
break;
case NSURLNetworkServiceTypeVideo:
networkServiceType = NSStreamNetworkServiceTypeVideo;
break;
case NSURLNetworkServiceTypeBackground:
networkServiceType = NSStreamNetworkServiceTypeBackground;
break;
case NSURLNetworkServiceTypeVoice:
networkServiceType = NSStreamNetworkServiceTypeVoice;
break;
#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 || __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000 || __TV_OS_VERSION_MAX_ALLOWED >= 100000 || __WATCH_OS_VERSION_MAX_ALLOWED >= 30000)
case NSURLNetworkServiceTypeCallSignaling:
networkServiceType = NSStreamNetworkServiceTypeCallSignaling;
break;
#endif
}
return [NSString stringWithFormat:@"Basic %@", userAndPasswordBase64Encoded];
return networkServiceType;
}
NS_ASSUME_NONNULL_END

View File

@ -1,20 +0,0 @@
BINS := SocketRocket.framework libSocketRocket.a
all: $(BINS)
HEADERS := SRWebSocket.h
SRCS := SRWebSocket.m
OBJS := $(SRCS:%.m=%.o)
CFLAGS += -fobjc-arc
libSocketRocket.a: $(OBJS)
$(AR) -rc $(AFLAGS) $@ $^
SocketRocket.framework: libSocketRocket.a
mkdir -p $@/Headers
cp -f $(HEADERS) $@/Headers
cp $^ $@/SocketRocket
clean:
rm -r $(OBJS) $(BINS)

View File

@ -10,9 +10,13 @@
//
#import "NSRunLoop+SRWebSocket.h"
#import "NSRunLoop+SRWebSocketPrivate.h"
#import "SRRunLoopThread.h"
// Required for object file to always be linked.
void import_NSRunLoop_SRWebSocket() { }
@implementation NSRunLoop (SRWebSocket)
+ (NSRunLoop *)SR_networkRunLoop

View File

@ -10,19 +10,28 @@
//
#import <Foundation/Foundation.h>
#import "SRSecurityPolicy.h"
NS_ASSUME_NONNULL_BEGIN
@interface NSURLRequest (SRWebSocket)
@property (nullable, nonatomic, retain, readonly) id<SRSecurityPolicy> SR_securityPolicy;
/**
An array of pinned `SecCertificateRef` SSL certificates that `SRWebSocket` will use for validation.
*/
@property (nullable, nonatomic, copy, readonly) NSArray *SR_SSLPinnedCertificates
DEPRECATED_MSG_ATTRIBUTE("Using pinned certificates is neither secure nor supported in SocketRocket, "
"and leads to security issues. Please use a proper, trust chain validated certificate.");
@end
@interface NSMutableURLRequest (SRWebSocket)
@property (nullable, nonatomic, retain) id<SRSecurityPolicy> SR_securityPolicy;
/**
An array of pinned `SecCertificateRef` SSL certificates that `SRWebSocket` will use for validation.
*/
@property (nullable, nonatomic, copy) NSArray *SR_SSLPinnedCertificates
DEPRECATED_MSG_ATTRIBUTE("Using pinned certificates is neither secure nor supported in SocketRocket, "
"and leads to security issues. Please use a proper, trust chain validated certificate.");
@end

View File

@ -10,30 +10,31 @@
//
#import "NSURLRequest+SRWebSocket.h"
#import "NSURLRequest+SRWebSocketPrivate.h"
// Required for object file to always be linked.
void import_NSURLRequest_SRWebSocket() { }
NS_ASSUME_NONNULL_BEGIN
NSString * const kSRSecurityPolicy = @"SRSecurityPolicy";
static NSString *const SRSSLPinnnedCertificatesKey = @"SocketRocket_SSLPinnedCertificates";
@implementation NSURLRequest (SRWebSocket)
- (nullable id<SRSecurityPolicy>)SR_securityPolicy;
- (nullable NSArray *)SR_SSLPinnedCertificates
{
return [NSURLProtocol propertyForKey:kSRSecurityPolicy inRequest:self];
return nil;
}
@end
@implementation NSMutableURLRequest (SRWebSocket)
- (void)setSR_securityPolicy:(nullable id<SRSecurityPolicy>)securityPolicy
- (void)setSR_SSLPinnedCertificates:(nullable NSArray *)SR_SSLPinnedCertificates
{
if (![securityPolicy respondsToSelector:@selector(evaluateServerTrust:forDomain:)]) {
@throw [NSException exceptionWithName:@"Assigning security policy failed."
reason:@"Trying to assign a security policy that doesn't respond to required selector"
userInfo:nil];
}
[NSURLProtocol setProperty:securityPolicy forKey:kSRSecurityPolicy inRequest:self];
[NSException raise:NSInvalidArgumentException
format:@"Using pinned certificates is neither secure nor supported in SocketRocket, "
"and leads to security issues. Please use a proper, trust chain validated certificate."];
}
@end

View File

@ -1,7 +1,5 @@
//
// Copyright 2012 Square Inc.
// Portions Copyright (c) 2016-present, Facebook, Inc.
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
@ -10,10 +8,65 @@
//
#import <Foundation/Foundation.h>
#import <Security/Security.h>
@protocol SRSecurityPolicy <NSObject>
NS_ASSUME_NONNULL_BEGIN
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
forDomain:(NSString*)domain;
@interface SRSecurityPolicy : NSObject
/**
A default `SRSecurityPolicy` implementation specifies socket security and
validates the certificate chain.
Use a subclass of `SRSecurityPolicy` for more fine grained customization.
*/
+ (instancetype)defaultPolicy;
/**
Specifies socket security and provider certificate pinning, disregarding certificate
chain validation.
@param pinnedCertificates Array of `SecCertificateRef` SSL certificates to use for validation.
*/
+ (instancetype)pinnningPolicyWithCertificates:(NSArray *)pinnedCertificates
DEPRECATED_MSG_ATTRIBUTE("Using pinned certificates is neither secure nor supported in SocketRocket, "
"and leads to security issues. Please use a proper, trust chain validated certificate.");
/**
Specifies socket security and optional certificate chain validation.
@param enabled Whether or not to validate the SSL certificate chain. If you
are considering using this method because your certificate was not issued by a
recognized certificate authority, consider using `pinningPolicyWithCertificates` instead.
*/
- (instancetype)initWithCertificateChainValidationEnabled:(BOOL)enabled
DEPRECATED_MSG_ATTRIBUTE("Disabling certificate chain validation is unsafe. "
"Please use a proper Certificate Authority to issue your TLS certificates.")
NS_DESIGNATED_INITIALIZER;
/**
Updates all the security options for input and output streams, for example you
can set your socket security level here.
@param stream Stream to update the options in.
*/
- (void)updateSecurityOptionsInStream:(NSStream *)stream;
/**
Whether or not the specified server trust should be accepted, based on the security policy.
This method should be used when responding to an authentication challenge from
a server. In the default implemenation, no further validation is done here, but
you're free to override it in a subclass. See `SRPinningSecurityPolicy.h` for
an example.
@param serverTrust The X.509 certificate trust of the server.
@param domain The domain of serverTrust.
@return Whether or not to trust the server.
*/
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,75 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "SRSecurityPolicy.h"
#import "SRPinningSecurityPolicy.h"
NS_ASSUME_NONNULL_BEGIN
@interface SRSecurityPolicy ()
@property (nonatomic, assign, readonly) BOOL certificateChainValidationEnabled;
@end
@implementation SRSecurityPolicy
+ (instancetype)defaultPolicy
{
return [self new];
}
+ (instancetype)pinnningPolicyWithCertificates:(NSArray *)pinnedCertificates
{
[NSException raise:NSInvalidArgumentException
format:@"Using pinned certificates is neither secure nor supported in SocketRocket, "
"and leads to security issues. Please use a proper, trust chain validated certificate."];
return nil;
}
- (instancetype)initWithCertificateChainValidationEnabled:(BOOL)enabled
{
self = [super init];
if (!self) { return self; }
_certificateChainValidationEnabled = enabled;
return self;
}
- (instancetype)init
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
return [self initWithCertificateChainValidationEnabled:YES];
#pragma clang diagnostic pop
}
- (void)updateSecurityOptionsInStream:(NSStream *)stream
{
// Enforce TLS 1.2
[stream setProperty:(__bridge id)CFSTR("kCFStreamSocketSecurityLevelTLSv1_2") forKey:(__bridge id)kCFStreamPropertySocketSecurityLevel];
// Validate certificate chain for this stream if enabled.
NSDictionary<NSString *, id> *sslOptions = @{ (__bridge NSString *)kCFStreamSSLValidatesCertificateChain : @(self.certificateChainValidationEnabled) };
[stream setProperty:sslOptions forKey:(__bridge NSString *)kCFStreamPropertySSLSettings];
}
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain
{
// No further evaluation happens in the default policy.
return YES;
}
@end
NS_ASSUME_NONNULL_END

View File

@ -10,7 +10,6 @@
//
#import <Foundation/Foundation.h>
#import <Security/SecCertificate.h>
NS_ASSUME_NONNULL_BEGIN
@ -21,8 +20,8 @@ typedef NS_ENUM(NSInteger, SRReadyState) {
SR_CLOSED = 3,
};
typedef enum SRStatusCode : NSInteger {
// 0999: Reserved and not used.
typedef NS_ENUM(NSInteger, SRStatusCode) {
// 0-999: Reserved and not used.
SRStatusCodeNormal = 1000,
SRStatusCodeGoingAway = 1001,
SRStatusCodeProtocolError = 1002,
@ -39,13 +38,14 @@ typedef enum SRStatusCode : NSInteger {
SRStatusCodeTryAgainLater = 1013,
// 1014: Reserved for future use by the WebSocket standard.
SRStatusCodeTLSHandshake = 1015,
// 10161999: Reserved for future use by the WebSocket standard.
// 20002999: Reserved for use by WebSocket extensions.
// 30003999: Available for use by libraries and frameworks. May not be used by applications. Available for registration at the IANA via first-come, first-serve.
// 40004999: Available for use by applications.
} SRStatusCode;
// 1016-1999: Reserved for future use by the WebSocket standard.
// 2000-2999: Reserved for use by WebSocket extensions.
// 3000-3999: Available for use by libraries and frameworks. May not be used by applications. Available for registration at the IANA via first-come, first-serve.
// 4000-4999: Available for use by applications.
};
@class SRWebSocket;
@class SRSecurityPolicy;
/**
Error domain used for errors reported by SRWebSocket.
@ -91,8 +91,10 @@ extern NSString *const SRHTTPResponseErrorKey;
/**
Current ready state of the socket. Default: `SR_CONNECTING`.
This property is Key-Value Observable and fully thread-safe.
*/
@property (nonatomic, assign, readonly) SRReadyState readyState;
@property (atomic, assign, readonly) SRReadyState readyState;
/**
An instance of `NSURL` that this socket connects to.
@ -131,6 +133,14 @@ extern NSString *const SRHTTPResponseErrorKey;
*/
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
/**
Initializes a web socket with a given `NSURLRequest`, specifying a transport security policy (e.g. SSL configuration).
@param request Request to initialize with.
@param securityPolicy Policy object describing transport security behavior.
*/
- (instancetype)initWithURLRequest:(NSURLRequest *)request securityPolicy:(SRSecurityPolicy *)securityPolicy;
/**
Initializes a web socket with a given `NSURLRequest` and list of sub-protocols.
@ -147,7 +157,17 @@ extern NSString *const SRHTTPResponseErrorKey;
@param allowsUntrustedSSLCertificates Boolean value indicating whether untrusted SSL certificates are allowed. Default: `false`.
*/
- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(nullable NSArray<NSString *> *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
NS_DESIGNATED_INITIALIZER;
DEPRECATED_MSG_ATTRIBUTE("Disabling certificate chain validation is unsafe. "
"Please use a proper Certificate Authority to issue your TLS certificates.");
/**
Initializes a web socket with a given `NSURLRequest`, list of sub-protocols and whether untrusted SSL certificates are allowed.
@param request Request to initialize with.
@param protocols An array of strings that turn into `Sec-WebSocket-Protocol`. Default: `nil`.
@param securityPolicy Policy object describing transport security behavior.
*/
- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(nullable NSArray<NSString *> *)protocols securityPolicy:(SRSecurityPolicy *)securityPolicy NS_DESIGNATED_INITIALIZER;
/**
Initializes a web socket with a given `NSURL`.
@ -164,6 +184,14 @@ NS_DESIGNATED_INITIALIZER;
*/
- (instancetype)initWithURL:(NSURL *)url protocols:(nullable NSArray<NSString *> *)protocols;
/**
Initializes a web socket with a given `NSURL`, specifying a transport security policy (e.g. SSL configuration).
@param url URL to initialize with.
@param securityPolicy Policy object describing transport security behavior.
*/
- (instancetype)initWithURL:(NSURL *)url securityPolicy:(SRSecurityPolicy *)securityPolicy;
/**
Initializes a web socket with a given `NSURL`, list of sub-protocols and whether untrusted SSL certificates are allowed.
@ -171,7 +199,9 @@ NS_DESIGNATED_INITIALIZER;
@param protocols An array of strings that turn into `Sec-WebSocket-Protocol`. Default: `nil`.
@param allowsUntrustedSSLCertificates Boolean value indicating whether untrusted SSL certificates are allowed. Default: `false`.
*/
- (instancetype)initWithURL:(NSURL *)url protocols:(nullable NSArray<NSString *> *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
- (instancetype)initWithURL:(NSURL *)url protocols:(nullable NSArray<NSString *> *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
DEPRECATED_MSG_ATTRIBUTE("Disabling certificate chain validation is unsafe. "
"Please use a proper Certificate Authority to issue your TLS certificates.");
/**
Unavailable initializer. Please use any other initializer.
@ -194,7 +224,7 @@ NS_DESIGNATED_INITIALIZER;
@param runLoop The run loop on which to schedule the receiver.
@param mode The mode for the run loop.
*/
- (void)scheduleInRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode;
- (void)scheduleInRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode NS_SWIFT_NAME(schedule(in:forMode:));
/**
Removes the receiver from a given run loop running in a given mode.
@ -202,7 +232,7 @@ NS_DESIGNATED_INITIALIZER;
@param runLoop The run loop on which the receiver was scheduled.
@param mode The mode for the run loop.
*/
- (void)unscheduleFromRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode;
- (void)unscheduleFromRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode NS_SWIFT_NAME(unschedule(from:forMode:));
///--------------------------------------
#pragma mark - Open / Close
@ -238,28 +268,55 @@ NS_DESIGNATED_INITIALIZER;
@deprecated Please use `sendString:` or `sendData` instead.
*/
- (void)send:(nullable id)message __attribute__((deprecated("Please use `sendString:` or `sendData` instead.")));
- (void)send:(nullable id)message __attribute__((deprecated("Please use `sendString:error:` or `sendData:error:` instead.")));
/**
Send a UTF-8 String to the server.
@param string String to send.
@param error On input, a pointer to variable for an `NSError` object.
If an error occurs, this pointer is set to an `NSError` object containing information about the error.
You may specify `nil` to ignore the error information.
@return `YES` if the string was scheduled to send, otherwise - `NO`.
*/
- (void)sendString:(NSString *)string;
- (BOOL)sendString:(NSString *)string error:(NSError **)error NS_SWIFT_NAME(send(string:));
/**
Send binary data to the server.
@param data Data to send.
@param data Data to send.
@param error On input, a pointer to variable for an `NSError` object.
If an error occurs, this pointer is set to an `NSError` object containing information about the error.
You may specify `nil` to ignore the error information.
@return `YES` if the string was scheduled to send, otherwise - `NO`.
*/
- (void)sendData:(nullable NSData *)data;
- (BOOL)sendData:(nullable NSData *)data error:(NSError **)error NS_SWIFT_NAME(send(data:));
/**
Send binary data to the server, without making a defensive copy of it first.
@param data Data to send.
@param error On input, a pointer to variable for an `NSError` object.
If an error occurs, this pointer is set to an `NSError` object containing information about the error.
You may specify `nil` to ignore the error information.
@return `YES` if the string was scheduled to send, otherwise - `NO`.
*/
- (BOOL)sendDataNoCopy:(nullable NSData *)data error:(NSError **)error NS_SWIFT_NAME(send(dataNoCopy:));
/**
Send Ping message to the server with optional data.
@param data Instance of `NSData` or `nil`.
@param data Instance of `NSData` or `nil`.
@param error On input, a pointer to variable for an `NSError` object.
If an error occurs, this pointer is set to an `NSError` object containing information about the error.
You may specify `nil` to ignore the error information.
@return `YES` if the string was scheduled to send, otherwise - `NO`.
*/
- (void)sendPing:(nullable NSData *)data;
- (BOOL)sendPing:(nullable NSData *)data error:(NSError **)error NS_SWIFT_NAME(sendPing(_:));
@end
@ -329,11 +386,19 @@ NS_DESIGNATED_INITIALIZER;
*/
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(nullable NSString *)reason wasClean:(BOOL)wasClean;
/**
Called on receive of a ping message from the server.
@param webSocket An instance of `SRWebSocket` that received a ping frame.
@param data Payload that was received or `nil` if there was no payload.
*/
- (void)webSocket:(SRWebSocket *)webSocket didReceivePingWithData:(nullable NSData *)data;
/**
Called when a pong data was received in response to ping.
@param webSocket An instance of `SRWebSocket` that received a pong frame.
@param pongPayload Payload that was received or `nil` if there was no payload.
@param webSocket An instance of `SRWebSocket` that received a pong frame.
@param pongData Payload that was received or `nil` if there was no payload.
*/
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(nullable NSData *)pongData;
@ -345,7 +410,7 @@ NS_DESIGNATED_INITIALIZER;
@return `YES` if text frame should be converted to UTF-8 String, otherwise - `NO`. Default: `YES`.
*/
- (BOOL)webSocketShouldConvertTextFrameToString:(SRWebSocket *)webSocket;
- (BOOL)webSocketShouldConvertTextFrameToString:(SRWebSocket *)webSocket NS_SWIFT_NAME(webSocketShouldConvertTextFrameToString(_:));
@end

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,15 @@
//
// Copyright 2012 Square Inc.
// Portions Copyright (c) 2016-present, Facebook, Inc.
//
//
// All rights reserved.
//
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <SocketRocket/SRWebSocket.h>
#import <SocketRocket/SRSecurityPolicy.h>
#import <SocketRocket/NSRunLoop+SRWebSocket.h>
#import <SocketRocket/NSURLRequest+SRWebSocket.h>
#import <SocketRocket/SRSecurityPolicy.h>
#import <SocketRocket/SRWebSocket.h>

View File

@ -11,6 +11,6 @@
@interface TCAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) UIWindow *window;
@end

View File

@ -11,51 +11,9 @@
@implementation TCAppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
@end

View File

@ -11,7 +11,7 @@
@interface TCViewController : UITableViewController
@property (nonatomic, retain) IBOutlet UITextView *inputView;
@property (nonatomic, strong) IBOutlet UITextView *inputView;
- (IBAction)reconnect:(id)sender;
- (IBAction)sendPing:(id)sender;

View File

@ -8,109 +8,137 @@
//
#import "TCViewController.h"
#import <SocketRocket/SRWebSocket.h>
#import <SocketRocket/SocketRocket.h>
#import "TCChatCell.h"
@interface TCMessage : NSObject
- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
- (instancetype)initWithMessage:(NSString *)message incoming:(BOOL)incoming;
@property (nonatomic, retain, readonly) NSString *message;
@property (nonatomic, readonly) BOOL fromMe;
@property (nonatomic, copy, readonly) NSString *message;
@property (nonatomic, assign, readonly, getter=isIncoming) BOOL incoming;
@end
@implementation TCMessage
@interface TCViewController () <SRWebSocketDelegate, UITextViewDelegate>
- (instancetype)initWithMessage:(NSString *)message incoming:(BOOL)incoming
{
self = [super init];
if (!self) return self;
@end
_incoming = incoming;
_message = message;
@implementation TCViewController {
SRWebSocket *_webSocket;
NSMutableArray *_messages;
return self;
}
@synthesize inputView = _inputView;
@end
#pragma mark - View lifecycle
@interface TCViewController () <SRWebSocketDelegate, UITextViewDelegate>
{
SRWebSocket *_webSocket;
NSMutableArray<TCMessage *> *_messages;
}
@end
@implementation TCViewController
///--------------------------------------
#pragma mark - View
///--------------------------------------
- (void)viewDidLoad;
{
[super viewDidLoad];
_messages = [[NSMutableArray alloc] init];
[self.tableView reloadData];
}
- (void)_reconnect;
{
_webSocket.delegate = nil;
[_webSocket close];
_webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://localhost:9000/chat"]]];
_webSocket.delegate = self;
self.title = @"Opening Connection...";
[_webSocket open];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self _reconnect];
[self reconnect:nil];
}
- (void)reconnect:(id)sender;
{
[self _reconnect];
}
- (void)sendPing:(id)sender;
{
[_webSocket sendPing:nil];
}
- (void)viewDidAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_inputView becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
_webSocket.delegate = nil;
[super viewDidDisappear:animated];
[_webSocket close];
_webSocket = nil;
}
///--------------------------------------
#pragma mark - Actions
///--------------------------------------
- (IBAction)reconnect:(id)sender
{
_webSocket.delegate = nil;
[_webSocket close];
_webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:@"wss://echo.websocket.org"]];
_webSocket.delegate = self;
self.title = @"Opening Connection...";
[_webSocket open];
}
- (void)sendPing:(id)sender;
{
[_webSocket sendPing:nil error:NULL];
}
///--------------------------------------
#pragma mark - Messages
///--------------------------------------
- (void)_addMessage:(TCMessage *)message
{
[_messages addObject:message];
[self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:_messages.count - 1 inSection:0] ]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
}
///--------------------------------------
#pragma mark - UITableViewController
///--------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _messages.count;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TCChatCell *chatCell = (id)cell;
TCMessage *message = [_messages objectAtIndex:indexPath.row];
chatCell.textView.text = message.message;
chatCell.nameLabel.text = message.fromMe ? @"Me" : @"Other";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
TCMessage *message = [_messages objectAtIndex:indexPath.row];
return [self.tableView dequeueReusableCellWithIdentifier:message.fromMe ? @"SentCell" : @"ReceivedCell"];
TCMessage *message = _messages[indexPath.row];
TCChatCell *cell = [self.tableView dequeueReusableCellWithIdentifier:message.incoming ? @"ReceivedCell" : @"SentCell"
forIndexPath:indexPath];
cell.textView.text = message.message;
cell.nameLabel.text = message.incoming ? @"Other" : @"Me";
return cell;
}
///--------------------------------------
#pragma mark - SRWebSocketDelegate
///--------------------------------------
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
{
@ -121,17 +149,15 @@
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
{
NSLog(@":( Websocket Failed With Error %@", error);
self.title = @"Connection Failed! (see logs)";
_webSocket = nil;
}
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(nonnull NSString *)string
{
NSLog(@"Received \"%@\"", message);
[_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:NO]];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
NSLog(@"Received \"%@\"", string);
[self _addMessage:[[TCMessage alloc] initWithMessage:string incoming:YES]];
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
@ -143,46 +169,27 @@
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
{
NSLog(@"Websocket received pong");
NSLog(@"WebSocket received pong");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
///--------------------------------------
#pragma mark - UITextViewDelegate
///--------------------------------------
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text rangeOfString:@"\n"].location != NSNotFound) {
NSString *message = [[textView.text stringByReplacingCharactersInRange:range withString:text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[_webSocket send:message];
[_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:YES]];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
NSString *message = [textView.text stringByReplacingCharactersInRange:range withString:text];
message = [message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
textView.text = @"";
[_webSocket sendString:message error:NULL];
[self _addMessage:[[TCMessage alloc] initWithMessage:message incoming:NO]];
textView.text = nil;
return NO;
}
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
{
return YES;
}
@end
@implementation TCMessage
@synthesize message = _message;
@synthesize fromMe = _fromMe;
- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
{
self = [super init];
if (self) {
_fromMe = fromMe;
_message = message;
}
return self;
}
@end

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="J5d-9g-n8O">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="J5d-9g-n8O">
<dependencies>
<deployment defaultVersion="1280" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
@ -89,7 +89,7 @@
</barButtonItem>
<barButtonItem key="rightBarButtonItem" systemItem="refresh" id="cNK-1A-b66">
<connections>
<action selector="reconnect:" destination="X5f-jW-I9m" id="V5L-sy-yng"/>
<action selector="reconnect:" destination="X5f-jW-I9m" id="yjV-1U-Tjn"/>
</connections>
</barButtonItem>
</navigationItem>

View File

@ -0,0 +1,7 @@
{
"url": "ws://127.0.0.1:9001",
"outdir": "./pages/results",
"cases": ["*"],
"exclude-cases": [],
"exclude-agent-cases": {}
}

View File

@ -1,21 +0,0 @@
#
# Copyright 2012 Square Inc.
# Portions Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE-examples file in the root directory of this source tree.
#
VIRTUALENV_PATH=$1
if [ -d "$VIRTUALENV_PATH" ]; then
echo "Virtual Env already installed"
else
python extern/virtualenv/virtualenv.py $VIRTUALENV_PATH
source $VIRTUALENV_PATH/bin/activate
pushd TestSupport/sr-testharness/
env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" pip install cryptography
python setup.py develop
popd
fi

View File

@ -1,37 +0,0 @@
#
# Copyright 2012 Square Inc.
# Portions Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE-examples file in the root directory of this source tree.
#
TEST_SCENARIOS=$1
TEST_URL=$2
CONFIGURATION=$3
export SR_TEST_URL=$TEST_URL
bash TestSupport/ensure_virtualenv.sh .env
pushd TestSupport/sr-testharness/
python setup.py develop
popd
source .env/bin/activate
sr-testharness -i '' -c "$TEST_SCENARIOS" &
CHILD_PID=$!
DESTINATION="OS=9.2,name=iPhone 6s"
SDK="iphonesimulator"
SHARED_ARGS="-configuration $CONFIGURATION -sdk $SDK"
xcodebuild -scheme SocketRocketTests -destination "$DESTINATION" $SHARED_ARGS TEST_AFTER_BUILD=YES clean build
RESULT=$?
kill $CHILD_PID
exit $RESULT

18
TestSupport/run_test_server.sh Executable file
View File

@ -0,0 +1,18 @@
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE-examples file in the root directory of this source tree.
#
PYENV_PATH=$(pwd)/.env
echo $PYENV_PATH
if [ -d "$PYENV_PATH" ]; then
source $PYENV_PATH/bin/activate
$PYENV_PATH/bin/wstest -m fuzzingserver -s TestSupport/autobahn_fuzzingserver.json
else
echo "Python Virtualenv not set up. Please run './TestSupport/setup_env.sh .env' first."
fi

36
TestSupport/setup_env.sh Executable file
View File

@ -0,0 +1,36 @@
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE-examples file in the root directory of this source tree.
#
VIRTUALENV_PATH=$1
VIRTUALENV_VERSION=15.0.1
if [ -d "$VIRTUALENV_PATH" ]; then
echo "Virtual Env already installed"
elif [ -z "$VIRTUALENV_PATH" ]; then
echo "Usage: ./setup_env.sh <folder path>"
else
mkdir $VIRTUALENV_PATH
pushd $VIRTUALENV_PATH
curl -L -o virtualenv.tar.gz https://pypi.python.org/packages/source/v/virtualenv/virtualenv-$VIRTUALENV_VERSION.tar.gz
tar xvfz virtualenv.tar.gz
pushd virtualenv-$VIRTUALENV_VERSION
python setup.py install --user
popd
popd
python $VIRTUALENV_PATH/virtualenv-$VIRTUALENV_VERSION/virtualenv.py $VIRTUALENV_PATH
source $VIRTUALENV_PATH/bin/activate
pip install autobahntestsuite
echo "Environment succesfully set up in $VIRTUALENV_PATH."
fi

View File

@ -1,3 +0,0 @@
[egg_info]
tag_build = dev
tag_svn_revision = true

View File

@ -1,31 +0,0 @@
from setuptools import setup, find_packages
import sys, os
version = '0.0'
setup(name='srtestharness',
version=version,
description="",
long_description="""\
""",
classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='',
author='',
author_email='',
url='',
license='',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=True,
install_requires=[
# -*- Extra requirements: -*-
'autobahntestsuite',
'autobahn==0.10.1',
'cryptography>=0.7'
],
entry_points="""
# -*- Entry points: -*-
[console_scripts]
sr-testharness = srtestharness.runner:main
""",
)

View File

@ -1,84 +0,0 @@
#
# Copyright 2012 Square Inc.
# Portions Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE-examples file in the root directory of this source tree.
#
import argparse
import json
import sys
import subprocess
import urlparse
from twisted.python import log
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File
from autobahntestsuite.fuzzing import FuzzingServerFactory
from autobahn.twisted.websocket import listenWS
class jsondict(dict):
def __init__(self, json_value):
if isinstance(json_value, dict):
dict.__init__(self, json_value)
else:
dict.__init__(self, json.loads(json_value))
def append(self, other):
self.update(jsondict(other))
def __repr__(self):
return "'%s'" % json.dumps(self)
def parse_opts(s):
return dict(s.split('='))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', default='ws://localhost:9001', help='Listen URL [default: %(default)s]')
parser.add_argument('-O', '--options', default=jsondict({'failByDrop':False}), type=jsondict, action='append', help='extra options (overwrites existing) [default: %(default)s]')
parser.add_argument('-p', '--webport', default=9090, type=int)
parser.add_argument('-i', '--listen-interface', default='localhost', help='interface to listen on')
parser.add_argument('-w', '--webdir', default='.')
parser.add_argument('-d', '--debug', default=False, action='store_true', help='Debug Mode [default: %(default)s]')
parser.add_argument('-o', '--outdir', default='./pages/results', metavar='DIR', help='Output Directory [default: %(default)s]')
parser.add_argument('-c', '--cases', default=['*'], nargs='+', help='test cases [default: %(default)s]')
parser.add_argument('-x', '--exclude-cases', default=[], nargs='+', help='test cases to exclude [default: %(default)s]')
parser.add_argument('-l', '--logfile', type=argparse.FileType('w'), default=sys.stdout, help='logging file [default: stdout]')
parser.add_argument('-t', '--exit-timeout', metavar='SECONDS', default=None, type=float, help='Will automatically exit after %(metavar)s seconds [default: %(default)s]')
args = parser.parse_args()
spec = args.__dict__
log.startLogging(args.logfile)
## fuzzing server
fuzzer = FuzzingServerFactory(spec)
listenWS(fuzzer, interface=args.listen_interface)
## web server
webdir = File(args.webdir)
web = Site(webdir)
reactor.listenTCP(args.webport, web, interface=args.listen_interface)
log.msg("Using Twisted reactor class %s" % str(reactor.__class__))
if args.exit_timeout:
def exit_callback():
log.msg("Exiting due to timeout (--exit-timeout/-t)")
reactor.fireSystemEvent('shutdown')
#reactor.stop()
sys.exit(12)
reactor.callLater(args.exit_timeout, exit_callback)
reactor.run()

View File

@ -70,10 +70,10 @@ SRAutobahnOperation *SRAutobahnTestOperation(NSURL *serverURL, NSInteger caseNum
caseNumber:@(caseNumber)
agent:agent
textMessageHandler:^(SRWebSocket * _Nonnull socket, NSString * _Nullable message) {
[socket sendString:message];
[socket sendString:message error:nil];
}
dataMessageHandler:^(SRWebSocket * _Nonnull socket, NSData * _Nullable message) {
[socket sendData:message];
[socket sendData:message error:nil];
}];
}

View File

@ -85,8 +85,8 @@
NSString *selectorName = [NSString stringWithFormat:@"Case #%@", identifier];
SEL selector = NSSelectorFromString(selectorName);
IMP implementation = imp_implementationWithBlock(^(SRAutobahnTests *self) {
[self performTestWithCaseNumber:caseNumber identifier:identifier];
IMP implementation = imp_implementationWithBlock(^(SRAutobahnTests *sself) {
[sself performTestWithCaseNumber:caseNumber identifier:identifier];
});
NSString *typeString = [NSString stringWithFormat:@"%s%s%s", @encode(id), @encode(id), @encode(SEL)];
class_addMethod(self, selector, implementation, typeString.UTF8String);

View File

@ -27,8 +27,7 @@ NSString *SRAutobahnTestAgentName(void)
NSURL *SRAutobahnTestServerURL(void)
{
NSString *serverURLString = [[NSProcessInfo processInfo].environment objectForKey:@"SR_TEST_URL"];
return [NSURL URLWithString:serverURLString];
return [NSURL URLWithString:@"ws://localhost:9001"];
}
///--------------------------------------

1
Vendor/xctoolchain vendored Submodule

@ -0,0 +1 @@
Subproject commit a66c63fe948cf8b4d77a51b69f35459fe0c68973

View File

@ -1,22 +0,0 @@
Copyright (c) 2007 Ian Bicking and Contributors
Copyright (c) 2009 Ian Bicking, The Open Planning Project
Copyright (c) 2011 The virtualenv developers
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because it is too large Load Diff

1
pages

@ -1 +0,0 @@
Subproject commit f103cccb1d138c74a02404dfefcb875b540286a1