Permalink
Browse files

Added HTTPMethod and HTTPStatusCode convenient methods to MKNetworkOp…

…eration
  • Loading branch information...
1 parent bcd49ae commit efdc9dcef0f150ff5eca4ac8e0f5486c4ccbb1f9 @MugunthKumar committed Dec 11, 2011
Showing with 114 additions and 9 deletions.
  1. +22 −0 MKNetworkKit/MKNetworkOperation.h
  2. +22 −9 MKNetworkKit/MKNetworkOperation.m
  3. +18 −0 ServerTests/basic_auth.php
  4. +52 −0 ServerTests/digest_auth.php
@@ -90,6 +90,28 @@ typedef void (^MKNKAuthBlock)(NSURLAuthenticationChallenge* challenge);
* This property is readonly cannot be updated.
*/
@property (nonatomic, strong, readonly) NSHTTPURLResponse *readonlyResponse;
+
+/*!
+ * @abstract The internal request object's method type
+ * @property HTTPMethod
+ *
+ * @discussion
+ * Returns the operation's method type
+ * This property is readonly cannot be modified.
+ * To create an operation with a new method type, use the operationWithURLString:params:httpMethod:
+ */
+@property (nonatomic, strong, readonly) NSString *HTTPMethod;
+
+/*!
+ * @abstract The internal response object's status code
+ * @property HTTPStatusCode
+ *
+ * @discussion
+ * Returns the operation's response's status code.
+ * Returns 0 when the operation has not yet started and the response is not available.
+ * This property is readonly cannot be modified.
+ */
+@property (nonatomic, assign, readonly) NSInteger HTTPStatusCode;
/*!
* @abstract String Encoding Property
* @property stringEncoding
@@ -143,12 +143,25 @@ -(NSString*) url {
-(NSMutableURLRequest*) readonlyRequest {
- return [self.request copy];
+ return self.request;
}
-(NSHTTPURLResponse*) readonlyResponse {
- return [self.response copy];
+ return self.response;
+}
+
+-(NSString*) HTTPMethod {
+
+ return self.request.HTTPMethod;
+}
+
+-(NSInteger) HTTPStatusCode {
+
+ if(self.response)
+ return self.response.statusCode;
+ else
+ return 0;
}
- (void)setFreezable:(BOOL)flag
@@ -372,18 +385,18 @@ -(void) setUsername:(NSString*) username password:(NSString*) password {
-(void) onCompletion:(MKNKResponseBlock) response onError:(MKNKErrorBlock) error {
- [self.responseBlocks addObject:[response copy]];
- [self.errorBlocks addObject:[error copy]];
+ [self.responseBlocks addObject:response];
+ [self.errorBlocks addObject:error];
}
-(void) onUploadProgressChanged:(MKNKProgressBlock) uploadProgressBlock {
- [self.uploadProgressChangedHandlers addObject:[uploadProgressBlock copy]];
+ [self.uploadProgressChangedHandlers addObject:uploadProgressBlock];
}
-(void) onDownloadProgressChanged:(MKNKProgressBlock) downloadProgressBlock {
- [self.downloadProgressChangedHandlers addObject:[downloadProgressBlock copy]];
+ [self.downloadProgressChangedHandlers addObject:downloadProgressBlock];
}
-(void) setDownloadStream:(NSOutputStream*) outputStream {
@@ -564,7 +577,7 @@ -(NSData*) bodyData {
if([self.filesToBePosted count] == 0 && [self.dataToBePosted count] == 0) {
- return [[[self.fieldsToBePosted urlEncodedKeyValueString] dataUsingEncoding:self.stringEncoding] mutableCopy];
+ return [[self.fieldsToBePosted urlEncodedKeyValueString] dataUsingEncoding:self.stringEncoding];
}
NSString *boundary = @"0xKhTmLbOuNdArY";
@@ -952,7 +965,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
DLog(@"%@ temporarily redirected", self.url);
}
else {
- DLog(@"%@ returned status %d", self.url, self.response.statusCode);
+ DLog(@"%@ returned status %ld", self.url, [self HTTPStatusCode]);
}
} else if (self.response.statusCode >= 400 && self.response.statusCode < 600) {
@@ -971,7 +984,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
-(NSData*) responseData {
if([self isFinished])
- return [self.mutableData copy];
+ return self.mutableData;
else if(self.cachedResponse)
return self.cachedResponse;
else
View
@@ -0,0 +1,18 @@
+<?php
+if (!isset($_SERVER['PHP_AUTH_USER'])) {
+ header('WWW-Authenticate: Basic realm="Test Realm"');
+ header('HTTP/1.0 401 Unauthorized');
+ echo 'You hit cancel';
+ exit;
+} else {
+ if ($_SERVER['PHP_AUTH_USER'] == 'admin' && $_SERVER['PHP_AUTH_PW'] == 'password')
+ {
+ echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
+ echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
+ }
+ else
+ {
+ echo "<p>Sorry, invalid crednetials</p>";
+ }
+}
+?>
@@ -0,0 +1,52 @@
+<?php
+$realm = 'Restricted area';
+
+//user => password
+$users = array('admin' => 'password', 'guest' => 'guest');
+
+
+if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
+ header('HTTP/1.1 401 Unauthorized');
+ header('WWW-Authenticate: Digest realm="'.$realm.
+ '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
+
+ die('Text to send if user hits Cancel button');
+}
+
+
+// analyze the PHP_AUTH_DIGEST variable
+if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
+ !isset($users[$data['username']]))
+ die('Wrong Credentials!');
+
+
+// generate the valid response
+$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
+$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
+$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
+
+if ($data['response'] != $valid_response)
+ die('Wrong Credentials!');
+
+// ok, valid username & password
+echo 'You are logged in as: ' . $data['username'];
+
+
+// function to parse the http auth header
+function http_digest_parse($txt)
+{
+ // protect against missing data
+ $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
+ $data = array();
+ $keys = implode('|', array_keys($needed_parts));
+
+ preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
+
+ foreach ($matches as $m) {
+ $data[$m[1]] = $m[3] ? $m[3] : $m[4];
+ unset($needed_parts[$m[1]]);
+ }
+
+ return $needed_parts ? false : $data;
+}
+?>

0 comments on commit efdc9dc

Please sign in to comment.