ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARConnection+Private.m
Go to the documentation of this file.
1 // ActiveResourceKit ARConnection+Private.m
2 //
3 // Copyright © 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import "ARConnection+Private.h"
26 
27 // for ARDimOf
28 #import "ARMacros.h"
29 
30 // for ARHTTPGetMethod and friends
31 #import "ARHTTPMethods.h"
32 
33 @implementation ARConnection(Private)
34 
35 - (NSDictionary *)defaultHeaders
36 {
37  return [NSDictionary dictionary];
38 }
39 
40 - (NSDictionary *)buildRequestHeaderFieldsUsingHeaders:(NSDictionary *)headers forHTTPMethod:(NSString *)HTTPMethod
41 {
42  NSMutableDictionary *headerFields = [NSMutableDictionary dictionaryWithDictionary:[self defaultHeaders]];
43  [headerFields addEntriesFromDictionary:[self HTTPFormatHeaderForHTTPMethod:HTTPMethod]];
44  if (headers)
45  {
46  [headerFields addEntriesFromDictionary:headers];
47  }
48  return [headerFields copy];
49 }
50 
51 //------------------------------------------------------------------------------
52 #pragma mark Format Header for Method
53 //------------------------------------------------------------------------------
54 
55 - (NSDictionary *)HTTPFormatHeaderForHTTPMethod:(NSString *)HTTPMethod
56 {
57  NSString *const HTTPMethods[] =
58  {
64  };
65  NSString *const headerNames[] =
66  {
67  @"Accept",
68  @"Content-Type",
69  @"Content-Type",
70  @"Accept",
71  @"Accept",
72  };
73  // Is this too ugly? A dictionary could implement the look-up. But that
74  // requires building a static dictionary initially and does not allow
75  // optimisation of searching. Using a simple linear look-up speeds up the
76  // more common request types, i.e. GET requests. There is a cost, that of
77  // slower look-up for less common types, e.g. HEAD. Is this a reasonable
78  // trade-off?
79  NSUInteger index;
80  for (index = 0; index < ARDimOf(HTTPMethods); index++)
81  {
82  if ([HTTPMethod isEqualToString:HTTPMethods[index]])
83  {
84  break;
85  }
86  }
87  NSDictionary *formatHeader;
88  if (index < ARDimOf(HTTPMethods))
89  {
90  formatHeader = [NSDictionary dictionaryWithObject:[[self format] MIMEType] forKey:headerNames[index]];
91  }
92  else
93  {
94  formatHeader = [NSDictionary dictionary];
95  }
96  return formatHeader;
97 }
98 
99 @end