API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPURLResponse.j
Go to the documentation of this file.
1 /*
2  * CPURLResponse.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 /*
25  CPURL _URL;
26  CPString _MIMEType;
27  unsigned _expectedContentLength;
28  CPString _textEncodingName;
29 */
37 @implementation CPURLResponse : CPObject
38 {
39  CPURL _URL;
40 }
41 
42 - (id)initWithURL:(CPURL)aURL
43 {
44  self = [super init];
45 
46  if (self)
47  _URL = aURL;
48 
49  return self;
50 }
51 
52 - (CPURL)URL
53 {
54  return _URL;
55 }
56 /*
57 Creating a Response
58 initWithURL:MIMEType:expectedContentLength:textEncodingName:
59 Getting the Response Properties
60 expectedContentLength
61 suggestedFilename
62 MIMEType
63 textEncodingName
64 URL
65 */
66 @end
67 
71 @implementation CPHTTPURLResponse : CPURLResponse
72 {
73  int _statusCode;
74  CPString _allResponseHeaders;
75  CPDictionary _responseHeaders;
76 }
77 
78 + (CPDictionary)parseHTTPHeaders:(CPString)headersString
79 {
81 
82  if (headersString)
83  {
84  var headerLines = headersString.split('\r\n'),
85  count = headerLines.length;
86 
87  while (count--)
88  {
89  var headerLine = headerLines[count],
90  index = headerLine.indexOf(': ');
91  if (index !== CPNotFound)
92  [r setValue:headerLine.substring(index + 2) forKey:headerLine.substring(0, index)];
93  }
94  }
95 
96  return r;
97 }
98 
99 /* @ignore */
100 - (void)_setStatusCode:(int)aStatusCode
101 {
102  _statusCode = aStatusCode;
103 }
104 
108 - (int)statusCode
109 {
110  return _statusCode;
111 }
112 
113 - (void)_setAllResponseHeaders:(CPString)responseHeadersString
114 {
115  _allResponseHeaders = responseHeadersString;
116 }
117 
121 - (CPDictionary)allHeaderFields
122 {
123  // Lazily parse the headers.
124  if (!_responseHeaders)
125  _responseHeaders = [[self class] parseHTTPHeaders:_allResponseHeaders];
126 
127  return _responseHeaders;
128 }
129 
130 @end