Skip to content

Commit

Permalink
Updated Socket.swift to 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Mar 13, 2018
1 parent b0aaaba commit 9ff704b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 34 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 BiAtoms
Copyright (c) 2018 BiAtoms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import PackageDescription
let package = Package(
name: "RequestSwift",
dependencies: [
.Package(url: "https://github.com/BiAtoms/Socket.swift.git", majorVersion: 2, minor: 0)
.Package(url: "https://github.com/BiAtoms/Socket.swift.git", majorVersion: 2, minor: 2)
]
)
8 changes: 3 additions & 5 deletions Podfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# Uncomment the next line to define a global platform for your project
platform :ios, '8.0' #links Foundation.framework to targets
platform :ios, '8.0' # links Foundation.framework for targets

target 'RequestSwift' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for RequestSwift
pod 'Socket.swift', '~> 2.0'
pod 'Socket.swift', '~> 2.2'

target 'RequestSwiftTests' do
inherit! :search_paths
end
end

#This is just from making project multiplatform. You should not use below code
# Below code is just used to make project multi-platform. You won't need it
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To integrate Request.swift into your Xcode project using CocoaPods, specify it i
```ruby
source 'https://github.com/CocoaPods/Specs.git'
target '<Your Target Name>' do
pod 'Request.swift' ~> '2.0'
pod 'Request.swift', '~> 2.1'
end
```

Expand Down
4 changes: 2 additions & 2 deletions Request.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Request.swift'
s.version = '2.0.0'
s.version = '2.1.0'
s.summary = 'A (sync/async) tiny http client written in swift.'
s.homepage = 'https://github.com/BiAtoms/Request.swift'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand All @@ -12,5 +12,5 @@ Pod::Spec.new do |s|
s.osx.deployment_target = '10.9'
s.tvos.deployment_target = '9.0'
s.source_files = 'Sources/*.swift'
s.dependency 'Socket.swift', '~> 2.0'
s.dependency 'Socket.swift', '~> 2.2'
end
36 changes: 12 additions & 24 deletions Sources/Requester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Dispatch
import SocketSwift

public typealias ResponseHandler = (Response?, Error?) -> Void
public typealias Proxy = (host: String, port: SocketSwift.Port)
public typealias Port = SocketSwift.Port
public typealias Proxy = (host: String, port: Port)
open class Requester {
open var timeout: Int
open var queue: DispatchQueue
Expand Down Expand Up @@ -61,50 +62,37 @@ open class Requester {
}
}


open func hostToIp( _ hostname: String) throws -> String {
if let a = gethostbyname(hostname) {
let b = withUnsafePointer(to: &(a.pointee.h_addr_list.pointee)) {
UnsafePointer<UnsafePointer<in_addr>>(OpaquePointer($0)).pointee.pointee
}
let c = inet_ntoa(b)!

return String(cString: c)
open func wait(_ socket: Socket) throws {
guard try socket.wait(for: .read, timeout: TimeInterval(timeout) / 1000) else {
throw Request.Error.timeout
}

throw Request.Error.couldntResolveHost
}

open func wait(_ socket: Socket) throws {
var fd = pollfd()
memset(&fd, 0, MemoryLayout<pollfd>.stride)
fd.fd = socket.fileDescriptor
fd.events = Int16(POLLIN)
if (try ing { poll(&fd, 1, Int32(timeout)) }) == 0 {
throw Request.Error.timeout
open func connect(_ socket: Socket, hostname: String, port: Port) throws {
guard let address = (try? socket.addresses(for: hostname, port: port))?.first else {
throw Request.Error.couldntResolveHost
}

try socket.connect(address: address)
}

open func connect(_ socket: Socket, _ request: Request) throws {
let (hostname, portString) = request.hostnameAndPort
if let proxy = self.proxy {
let address = try hostToIp(proxy.host)
try socket.connect(port: proxy.port, address: address)
try connect(socket, hostname: proxy.host, port: proxy.port)
let to = hostname + ":" + (portString ?? "80")
try socket.write("CONNECT \(to) HTTP/1.0\r\n\r\n".bytes)
try wait(socket)
let response = try ResponseParser.parse(socket: socket)
if response.statusCode != 200 {
throw Request.Error.proxyConnectionFailed
}

} else {
var port: SocketSwift.Port = 80
if let p = portString {
port = Port(p)!
}
let address = try hostToIp(hostname)
try socket.connect(port: port, address: address)
try connect(socket, hostname: hostname, port: port)
}
}

Expand Down

0 comments on commit 9ff704b

Please sign in to comment.