Toggle navigation
Toggle navigation
This project
Loading...
Sign in
iOS
/
asi-http-request
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Ben Copsey
2009-08-10 21:43:56 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b6b6faaee44a8dfd9f6e3116c9b7653206b4943d
b6b6faae
1 parent
9e39464a
Tweaks, remove debug stuff
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
22 deletions
Classes/ASIHTTPRequest.h
Classes/ASIHTTPRequest.m
Classes/ASIInputStream.h
Classes/ASIInputStream.m
iPhone.xcodeproj/project.pbxproj
Classes/ASIHTTPRequest.h
View file @
b6b6faa
...
...
@@ -455,10 +455,10 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// Only works on Mac OS, will always return 'application/octet-stream' on iPhone
+
(
NSString
*
)
mimeTypeForFileAtPath
:(
NSString
*
)
path
;
#pragma mark bandwidth throttling
#pragma mark bandwidth
measurement /
throttling
// The maximum number of bytes ALL requests can send / receive in a second
// This is a rough figure. The actual amount used
may be slightly more
// This is a rough figure. The actual amount used
will be slightly more, this does not include HTTP headers
+
(
unsigned
long
)
maxBandwidthPerSecond
;
+
(
void
)
setMaxBandwidthPerSecond
:(
unsigned
long
)
bytes
;
...
...
Classes/ASIHTTPRequest.m
View file @
b6b6faa
...
...
@@ -265,7 +265,7 @@ BOOL shouldThrottleBandwidth = NO;
if
(
!
[
requestMethod
isEqualToString
:
@"POST"
]
&&
!
[
requestMethod
isEqualToString
:
@"PUT"
])
{
[
self
setRequestMethod
:
@"POST"
];
}
//
[self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]];
[
self
addRequestHeader
:
@"Content-Length"
value
:[
NSString
stringWithFormat
:
@"%llu"
,[
self
postLength
]]];
}
[
self
setHaveBuiltPostBody
:
YES
];
}
...
...
@@ -525,7 +525,9 @@ BOOL shouldThrottleBandwidth = NO;
if
(
!
[
self
downloadDestinationPath
])
{
[
self
setRawResponseData
:[[[
NSMutableData
alloc
]
init
]
autorelease
]];
}
// Create the stream for the request.
// Create the stream for the request
// Do we need to stream the request body from disk
if
([
self
shouldStreamPostDataFromDisk
]
&&
[
self
postBodyFilePath
]
&&
[[
NSFileManager
defaultManager
]
fileExistsAtPath
:[
self
postBodyFilePath
]])
{
// Are we gzipping the request body?
...
...
@@ -536,7 +538,14 @@ BOOL shouldThrottleBandwidth = NO;
}
readStream
=
CFReadStreamCreateForStreamedHTTPRequest
(
kCFAllocatorDefault
,
request
,(
CFReadStreamRef
)[
self
postBodyReadStream
]);
}
else
{
readStream
=
CFReadStreamCreateForHTTPRequest
(
kCFAllocatorDefault
,
request
);
// If we have a request body, we'll stream it from memory using our custom stream, so that it can be bandwidth-throttled if nescessary
if
([
self
postBody
])
{
[
self
setPostBodyReadStream
:[
ASIInputStream
inputStreamWithData
:[
self
postBody
]]];
readStream
=
CFReadStreamCreateForStreamedHTTPRequest
(
kCFAllocatorDefault
,
request
,(
CFReadStreamRef
)[
self
postBodyReadStream
]);
}
else
{
readStream
=
CFReadStreamCreateForHTTPRequest
(
kCFAllocatorDefault
,
request
);
}
}
if
(
!
readStream
)
{
[[
self
cancelledLock
]
unlock
];
...
...
@@ -659,8 +668,6 @@ BOOL shouldThrottleBandwidth = NO;
NSDate
*
now
=
[
NSDate
date
];
//NSLog(@"loop");
// See if we need to timeout
if
(
lastActivityTime
&&
timeOutSeconds
>
0
&&
[
now
timeIntervalSinceDate
:
lastActivityTime
]
>
timeOutSeconds
)
{
...
...
@@ -698,9 +705,6 @@ BOOL shouldThrottleBandwidth = NO;
// Find out if we've sent any more data than last time, and reset the timeout if so
if
(
totalBytesSent
>
lastBytesSent
)
{
// // For bandwidth measurement / throttling
// [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(totalBytesSent-lastBytesSent)];
[
self
setLastActivityTime
:[
NSDate
date
]];
[
self
setLastBytesSent
:
totalBytesSent
];
}
...
...
@@ -2313,7 +2317,7 @@ BOOL shouldThrottleBandwidth = NO;
#endif
}
#pragma mark bandwidth throttling
#pragma mark bandwidth
measurement /
throttling
+
(
BOOL
)
shouldThrottleBandwidth
{
...
...
@@ -2355,7 +2359,6 @@ BOOL shouldThrottleBandwidth = NO;
+
(
void
)
recordBandwidthUsage
{
//NSLog(@"--Mark-- %lu",bandwidthUsedInLastSecond);
if
(
bandwidthUsedInLastSecond
==
0
)
{
[
bandwidthUsageTracker
removeAllObjects
];
}
else
{
...
...
@@ -2382,9 +2385,9 @@ BOOL shouldThrottleBandwidth = NO;
{
[
bandwidthThrottlingLock
lock
];
//
if (!bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < 0) {
//
[self recordBandwidthUsage];
//
}
if
(
!
bandwidthMeasurementDate
||
[
bandwidthMeasurementDate
timeIntervalSinceNow
]
<
0
)
{
[
self
recordBandwidthUsage
];
}
unsigned
long
amount
=
averageBandwidthUsedPerSecond
;
[
bandwidthThrottlingLock
unlock
];
return
amount
;
...
...
@@ -2403,9 +2406,7 @@ BOOL shouldThrottleBandwidth = NO;
if
(
maxBandwidthPerSecond
>
0
)
{
// How much data can we still send or receive this second?
long
long
bytesRemaining
=
(
long
long
)
maxBandwidthPerSecond
-
(
long
long
)
bandwidthUsedInLastSecond
;
//NSLog(@"%qi",bytesRemaining);
// Have we used up our allowance?
if
(
bytesRemaining
<
8
)
{
...
...
@@ -2458,7 +2459,6 @@ BOOL shouldThrottleBandwidth = NO;
if
(
maxBandwidthPerSecond
)
{
toRead
=
maxBandwidthPerSecond
/
32
;
}
//NSLog(@"max: %lu used: %lu",maxBandwidthPerSecond,bandwidthUsedInLastSecond);
if
(
maxBandwidthPerSecond
>
0
&&
(
bandwidthUsedInLastSecond
+
toRead
>
maxBandwidthPerSecond
))
{
toRead
=
0
;
}
...
...
Classes/ASIInputStream.h
View file @
b6b6faa
...
...
@@ -8,11 +8,14 @@
#import <Foundation/Foundation.h>
// This is a wrapper for NSInputStream that pretends to be an NSInputStream itself
// Subclassing NSInputStream seems to be tricky, and may involve overriding undocumented methods, so we'll cheat instead.
@interface
ASIInputStream
:
NSObject
{
NSInputStream
*
stream
;
}
+
(
id
)
inputStreamWithFileAtPath
:
(
NSString
*
)
path
;
+
(
id
)
inputStreamWithData
:(
NSData
*
)
data
;
@property
(
retain
)
NSInputStream
*
stream
;
@end
...
...
Classes/ASIInputStream.m
View file @
b6b6faa
...
...
@@ -9,9 +9,6 @@
#import "ASIInputStream.h"
#import "ASIHTTPRequest.h"
// This is a wrapper for NSInputStream that pretends to be an NSInputStream itself
// Subclassing NSInputStream seems to be tricky, and may involve overriding undocumented methods, so we'll cheat instead.
@implementation
ASIInputStream
+
(
id
)
inputStreamWithFileAtPath
:(
NSString
*
)
path
...
...
@@ -21,6 +18,13 @@
return
stream
;
}
+
(
id
)
inputStreamWithData
:(
NSData
*
)
data
{
ASIInputStream
*
stream
=
[[[
self
alloc
]
init
]
autorelease
];
[
stream
setStream
:[
NSInputStream
inputStreamWithData
:
data
]];
return
stream
;
}
-
(
void
)
dealloc
{
[
stream
release
];
...
...
@@ -30,8 +34,10 @@
-
(
BOOL
)
hasBytesAvailable
{
if
([
ASIHTTPRequest
maxUploadReadLength
]
==
0
)
{
NSLog
(
@"no"
);
return
NO
;
}
NSLog
(
@"yes"
);
return
[[
self
stream
]
hasBytesAvailable
];
}
...
...
iPhone.xcodeproj/project.pbxproj
View file @
b6b6faa
This diff was suppressed by a .gitattributes entry.
Please
register
or
login
to post a comment