Skip to content

Commit abd0704

Browse files
committed
Output a string representation of the HTTP status code in the HTTP operation log. Add RKStringFromStatusCode convenience method to the HTTP utilities
1 parent 280fd5d commit abd0704

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

Code/Network/RKHTTPRequestOperation.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,19 @@ - (void)HTTPOperationDidStart:(NSNotification *)notification
147147
- (void)HTTPOperationDidFinish:(NSNotification *)notification
148148
{
149149
RKHTTPRequestOperation *operation = [notification object];
150+
NSString *statusCodeString = RKStringFromStatusCode([operation.response statusCode]);
151+
NSString *statusCodeFragment = statusCodeString ? [NSString stringWithFormat:@"(%ld %@)", (long)[operation.response statusCode], statusCodeString] : [NSString stringWithFormat:@"(%ld)", (long)[operation.response statusCode]];
150152
if (operation.error) {
151153
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
152-
RKLogError(@"%@ '%@' (%ld):\nerror=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode], operation.error, operation.responseString);
154+
RKLogError(@"%@ '%@' %@:\nerror=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, operation.error, operation.responseString);
153155
} else {
154-
RKLogError(@"%@ '%@' (%ld): %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode], operation.error);
156+
RKLogError(@"%@ '%@' %@: %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, operation.error);
155157
}
156158
} else {
157159
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
158-
RKLogTrace(@"%@ '%@' (%ld):\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode], [operation.response allHeaderFields], RKLogTruncateString(operation.responseString));
160+
RKLogTrace(@"%@ '%@' %@:\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, [operation.response allHeaderFields], RKLogTruncateString(operation.responseString));
159161
} else {
160-
RKLogInfo(@"%@ '%@' (%ld)", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode]);
162+
RKLogInfo(@"%@ '%@' %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment);
161163
}
162164
}
163165
}

Code/ObjectMapping/RKHTTPUtilities.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ NSRange RKStatusCodeRangeForClass(RKStatusCodeClass statusCodeClass);
7878
*/
7979
NSIndexSet *RKStatusCodeIndexSetForClass(RKStatusCodeClass statusCodeClass);
8080

81-
// TODO: Implement these guys...
82-
//NSString * RKStringFromStatusCode(NSInteger statusCode);
83-
//NSInteger RKStatusCodeFromString(NSString *statusCode);
81+
/**
82+
Returns string representation of a given HTTP status code.
83+
84+
The list of supported status codes was built from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
85+
86+
@param statusCode The HTTP status code to return a string from.
87+
@return A string representation of the given status code.
88+
*/
89+
NSString *RKStringFromStatusCode(NSInteger statusCode);
8490

8591
/**
8692
Parse HTTP Date: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

Code/ObjectMapping/RKHTTPUtilities.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,97 @@ RKRequestMethod RKRequestMethodFromString(NSString *methodName)
5959
else return RKRequestMethodInvalid;
6060
}
6161

62+
// Built from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
63+
static NSDictionary *RKStatusCodesToNamesDictionary()
64+
{
65+
static NSDictionary *statusCodesToNamesDictionary = nil;
66+
static dispatch_once_t onceToken;
67+
dispatch_once(&onceToken, ^{
68+
statusCodesToNamesDictionary = @{
69+
// 1xx (Informational)
70+
@(100): @"Continue",
71+
@(101): @"Switching Protocols",
72+
@(102): @"Processing",
73+
74+
// 2xx (Success)
75+
@(200): @"OK",
76+
@(201): @"Created",
77+
@(202): @"Accepted",
78+
@(203): @"Non-Authoritative Information",
79+
@(204): @"No Content",
80+
@(205): @"Reset Content",
81+
@(206): @"Partial Content",
82+
@(207): @"Multi-Status",
83+
@(208): @"Already Reported",
84+
@(226): @"IM Used",
85+
86+
// 3xx (Redirection)
87+
@(300): @"Multiple Choices",
88+
@(301): @"Moved Permanently",
89+
@(302): @"Found",
90+
@(303): @"See Other",
91+
@(304): @"Not Modified",
92+
@(305): @"Use Proxy",
93+
@(306): @"Switch Proxy",
94+
@(307): @"Temporary Redirect",
95+
@(308): @"Permanent Redirect",
96+
97+
// 4xx (Client Error)
98+
@(400): @"Bad Request",
99+
@(401): @"Unauthorized",
100+
@(402): @"Payment Required",
101+
@(403): @"Forbidden",
102+
@(404): @"Not Found",
103+
@(405): @"Method Not Allowed",
104+
@(406): @"Not Acceptable",
105+
@(407): @"Proxy Authentication Required",
106+
@(408): @"Request Timeout",
107+
@(409): @"Conflict",
108+
@(410): @"Gone",
109+
@(411): @"Length Required",
110+
@(412): @"Precondition Failed",
111+
@(413): @"Request Entity Too Large",
112+
@(414): @"Request-URI Too Long",
113+
@(415): @"Unsupported Media Type",
114+
@(416): @"Requested Range Not Satisfiable",
115+
@(417): @"Expectation Failed",
116+
@(418): @"I'm a teapot",
117+
@(420): @"Enhance Your Calm",
118+
@(422): @"Unprocessable Entity",
119+
@(423): @"Locked",
120+
@(424): @"Failed Dependency",
121+
@(424): @"Method Failure",
122+
@(425): @"Unordered Collection",
123+
@(426): @"Upgrade Required",
124+
@(428): @"Precondition Required",
125+
@(429): @"Too Many Requests",
126+
@(431): @"Request Header Fields Too Large",
127+
@(451): @"Unavailable For Legal Reasons",
128+
129+
// 5xx (Server Error)
130+
@(500): @"Internal Server Error",
131+
@(501): @"Not Implemented",
132+
@(502): @"Bad Gateway",
133+
@(503): @"Service Unavailable",
134+
@(504): @"Gateway Timeout",
135+
@(505): @"HTTP Version Not Supported",
136+
@(506): @"Variant Also Negotiates",
137+
@(507): @"Insufficient Storage",
138+
@(508): @"Loop Detected",
139+
@(509): @"Bandwidth Limit Exceeded",
140+
@(510): @"Not Extended",
141+
@(511): @"Network Authentication Required",
142+
};
143+
});
144+
return statusCodesToNamesDictionary;
145+
}
146+
147+
NSString * RKStringFromStatusCode(NSInteger statusCode)
148+
{
149+
return [RKStatusCodesToNamesDictionary() objectForKey:@(statusCode)];
150+
}
151+
152+
62153
/**
63154
Below is ragel source used to compile those tables. The output was polished / pretty-printed and tweaked from ragel.
64155
As the generated code is "hard" to debug, we store the code in http-date.r1

0 commit comments

Comments
 (0)