Skip to content
This repository
Browse code

Added HTTPMethod and HTTPStatusCode convenient methods to MKNetworkOp…

…eration
  • Loading branch information...
commit efdc9dcef0f150ff5eca4ac8e0f5486c4ccbb1f9 1 parent bcd49ae
Mugunth Kumar authored
22 MKNetworkKit/MKNetworkOperation.h
@@ -90,6 +90,28 @@ typedef void (^MKNKAuthBlock)(NSURLAuthenticationChallenge* challenge);
90 90 * This property is readonly cannot be updated.
91 91 */
92 92 @property (nonatomic, strong, readonly) NSHTTPURLResponse *readonlyResponse;
  93 +
  94 +/*!
  95 + * @abstract The internal request object's method type
  96 + * @property HTTPMethod
  97 + *
  98 + * @discussion
  99 + * Returns the operation's method type
  100 + * This property is readonly cannot be modified.
  101 + * To create an operation with a new method type, use the operationWithURLString:params:httpMethod:
  102 + */
  103 +@property (nonatomic, strong, readonly) NSString *HTTPMethod;
  104 +
  105 +/*!
  106 + * @abstract The internal response object's status code
  107 + * @property HTTPStatusCode
  108 + *
  109 + * @discussion
  110 + * Returns the operation's response's status code.
  111 + * Returns 0 when the operation has not yet started and the response is not available.
  112 + * This property is readonly cannot be modified.
  113 + */
  114 +@property (nonatomic, assign, readonly) NSInteger HTTPStatusCode;
93 115 /*!
94 116 * @abstract String Encoding Property
95 117 * @property stringEncoding
31 MKNetworkKit/MKNetworkOperation.m
@@ -143,12 +143,25 @@ -(NSString*) url {
143 143
144 144 -(NSMutableURLRequest*) readonlyRequest {
145 145
146   - return [self.request copy];
  146 + return self.request;
147 147 }
148 148
149 149 -(NSHTTPURLResponse*) readonlyResponse {
150 150
151   - return [self.response copy];
  151 + return self.response;
  152 +}
  153 +
  154 +-(NSString*) HTTPMethod {
  155 +
  156 + return self.request.HTTPMethod;
  157 +}
  158 +
  159 +-(NSInteger) HTTPStatusCode {
  160 +
  161 + if(self.response)
  162 + return self.response.statusCode;
  163 + else
  164 + return 0;
152 165 }
153 166
154 167 - (void)setFreezable:(BOOL)flag
@@ -372,18 +385,18 @@ -(void) setUsername:(NSString*) username password:(NSString*) password {
372 385
373 386 -(void) onCompletion:(MKNKResponseBlock) response onError:(MKNKErrorBlock) error {
374 387
375   - [self.responseBlocks addObject:[response copy]];
376   - [self.errorBlocks addObject:[error copy]];
  388 + [self.responseBlocks addObject:response];
  389 + [self.errorBlocks addObject:error];
377 390 }
378 391
379 392 -(void) onUploadProgressChanged:(MKNKProgressBlock) uploadProgressBlock {
380 393
381   - [self.uploadProgressChangedHandlers addObject:[uploadProgressBlock copy]];
  394 + [self.uploadProgressChangedHandlers addObject:uploadProgressBlock];
382 395 }
383 396
384 397 -(void) onDownloadProgressChanged:(MKNKProgressBlock) downloadProgressBlock {
385 398
386   - [self.downloadProgressChangedHandlers addObject:[downloadProgressBlock copy]];
  399 + [self.downloadProgressChangedHandlers addObject:downloadProgressBlock];
387 400 }
388 401
389 402 -(void) setDownloadStream:(NSOutputStream*) outputStream {
@@ -564,7 +577,7 @@ -(NSData*) bodyData {
564 577
565 578 if([self.filesToBePosted count] == 0 && [self.dataToBePosted count] == 0) {
566 579
567   - return [[[self.fieldsToBePosted urlEncodedKeyValueString] dataUsingEncoding:self.stringEncoding] mutableCopy];
  580 + return [[self.fieldsToBePosted urlEncodedKeyValueString] dataUsingEncoding:self.stringEncoding];
568 581 }
569 582
570 583 NSString *boundary = @"0xKhTmLbOuNdArY";
@@ -952,7 +965,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
952 965 DLog(@"%@ temporarily redirected", self.url);
953 966 }
954 967 else {
955   - DLog(@"%@ returned status %d", self.url, self.response.statusCode);
  968 + DLog(@"%@ returned status %ld", self.url, [self HTTPStatusCode]);
956 969 }
957 970
958 971 } else if (self.response.statusCode >= 400 && self.response.statusCode < 600) {
@@ -971,7 +984,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
971 984 -(NSData*) responseData {
972 985
973 986 if([self isFinished])
974   - return [self.mutableData copy];
  987 + return self.mutableData;
975 988 else if(self.cachedResponse)
976 989 return self.cachedResponse;
977 990 else
18 ServerTests/basic_auth.php
... ... @@ -0,0 +1,18 @@
  1 +<?php
  2 +if (!isset($_SERVER['PHP_AUTH_USER'])) {
  3 + header('WWW-Authenticate: Basic realm="Test Realm"');
  4 + header('HTTP/1.0 401 Unauthorized');
  5 + echo 'You hit cancel';
  6 + exit;
  7 +} else {
  8 + if ($_SERVER['PHP_AUTH_USER'] == 'admin' && $_SERVER['PHP_AUTH_PW'] == 'password')
  9 + {
  10 + echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
  11 + echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
  12 + }
  13 + else
  14 + {
  15 + echo "<p>Sorry, invalid crednetials</p>";
  16 + }
  17 +}
  18 +?>
52 ServerTests/digest_auth.php
... ... @@ -0,0 +1,52 @@
  1 +<?php
  2 +$realm = 'Restricted area';
  3 +
  4 +//user => password
  5 +$users = array('admin' => 'password', 'guest' => 'guest');
  6 +
  7 +
  8 +if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
  9 + header('HTTP/1.1 401 Unauthorized');
  10 + header('WWW-Authenticate: Digest realm="'.$realm.
  11 + '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
  12 +
  13 + die('Text to send if user hits Cancel button');
  14 +}
  15 +
  16 +
  17 +// analyze the PHP_AUTH_DIGEST variable
  18 +if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
  19 + !isset($users[$data['username']]))
  20 + die('Wrong Credentials!');
  21 +
  22 +
  23 +// generate the valid response
  24 +$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
  25 +$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
  26 +$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
  27 +
  28 +if ($data['response'] != $valid_response)
  29 + die('Wrong Credentials!');
  30 +
  31 +// ok, valid username & password
  32 +echo 'You are logged in as: ' . $data['username'];
  33 +
  34 +
  35 +// function to parse the http auth header
  36 +function http_digest_parse($txt)
  37 +{
  38 + // protect against missing data
  39 + $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
  40 + $data = array();
  41 + $keys = implode('|', array_keys($needed_parts));
  42 +
  43 + preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
  44 +
  45 + foreach ($matches as $m) {
  46 + $data[$m[1]] = $m[3] ? $m[3] : $m[4];
  47 + unset($needed_parts[$m[1]]);
  48 + }
  49 +
  50 + return $needed_parts ? false : $data;
  51 +}
  52 +?>

0 comments on commit efdc9dc

Please sign in to comment.
Something went wrong with that request. Please try again.