Class WOResponse

CLASS DESCRIPTION

Inherits From:
NSObject

Adopts:
NSCopying

A WOResponse object represents an HTTP response that an application returns to a Web server to complete a cycle of the request-response loop. The composition of a response occurs during the third and final phase of this loop, a phase marked by the propagation of the appendToResponse:inContext: message through the objects of the application. The WOApplication object first sends this message, passing in a newly-created WOResponse object as an argument. WOElement objects, which represent the dynamic and static HTML elements on a page, respond to the message by appending their HTML representation to the content of the WOResponse object. WOApplication, WOSession, and WOComponent objects can also respond to the message by adding information to the WOResponse object.

A WOResponse has two major parts: HTML content and HTTP information. The content is what is displayed in a Web browser; it can include escaped HTML, which is HTML code shown "as is," uninterpreted. The other information encapsulated by a WOResponse object is in the handling the response. This HTTP data includes headers, status codes, and version string. See the HTTP specification or HTTP documentation for descriptions of these items.

As you might expect, the methods of the WOResponse class can be divided into two groups, those that add to a response's HTML content and those that read and set HTTP information. The former group consists of methods that escape HTML (appendContentHTMLAttributeValue: and appendContentHTMLString:) and those that don't. For images and other binary data, you can use the appendContentData:. You can obtain and set the entire content of the response with content and setContent:. The following example shows a sequence of "appendContent" messages that compose an HTTP "POST" message:

    [aResponse appendContentString:@"<form method=\"POST\" action=\""];

    [aResponse appendContentHTMLAttributeValue:[aContext url]];

    [aResponse appendContentCharacter:'"'];

    [aResponse.appendContentString:@">"];

Most of the remaining WOResponse methods set and read the response's HTTP headers, the HTTP status code, and the HTTP version.

Content Encodings

You can set the string encoding used for the response content with setContentEncoding: and you find out what the current encoding is with contentEncoding. An integer represents the type of encoding. The following table lists these integer values along with their OPENSTEP string-constant names.

int
Value
OPENSTEP NameNotes
1NSASCIIStringEncoding0 through 127
2NSNEXTSTEPStringEncoding
3NSJapaneseEUCStringEncoding
4NSUTF8StringEncoding
5NSISOLatin1StringEncodingdefault
6NSSymbolStringEncoding
7NSNonLossyASCIIStringEncoding7-bit verbose ASCII to represent all unichars
8NSShiftJISStringEncoding
9NSISOLatin2StringEncoding
10NSUnicodeStringEncoding
11NSWindowsCP1251StringEncodingCyrillic; same as AdobeStandardCyrillic
12NSWindowsCP1252StringEncodingWindows Latin1
13NSWindowsCP1253StringEncodingWindows Greek
14NSWindowsCP1254StringEncodingWindows Turkish
15NSWindowsCP1250StringEncodingWindows Latin2
21NSISO2022JPStringEncoding ISO 2022 Japanese encoding for electronic mail


INSTANCE METHODS

appendContentBytes:length:

- (void)appendContentBytes:(const void *)someBytes length:(unsigned)length

Appends length number of bytes pointed to by someBytes to the HTTP response.


appendContentCharacter:

- (void)appendContentCharacter:(char)aChar

Appends a single ASCII character (aChar) to the HTTP response.

Example:

    // ...

    if (aFlag)

        [aResponse appendContentCharacter:'Y'];

    else

        [aResponse appendContentCharacter:'N'];


appendContentData:

- (void)appendContentData:(NSData *)dataObject

Appends a data-encapsulating object (dataObject) to the HTTP response.


appendContentHTMLAttributeValue:

- (void)appendContentHTMLAttributeValue:(NSString *)aValue

Appends an HTML attribute value to the HTTP content after transforming the string aValue into an NSData object using the receiver's content encoding. Special HTML characters ("<", ">", "&", and double quote) are escaped so that the browser does not interpret them. In other words, the message

    aResponse.appendContentHTMLAttribute("<B>"); 

would transform the argument to "&lt;B&gt;".

See Also: - setContentEncoding:


appendContentHTMLString:

- (void)appendContentHTMLString:(NSString *)aString

Appends an HTML string to the HTTP response after transforming the string aString into an NSData object using the receiver's content encoding. Special HTML characters ("<", ">", "&", and double quote) are escaped so that the browser does not interpret them. For example, "<P>" becomes "&ltP&gt".

See Also: - setContentEncoding:


appendContentString:

- (void)appendContentString:(NSString *)aString

Appends a string to the content of the HTTP response. The string is transformed into an NSData object using the receiver's content encoding. The special HTML characters "<", ">", "&", and double-quote are not escaped so the browser can interpret them as HTML.


content

- (NSData *)content

Returns the HTML content of the receiver as an NSData object. An exception is raised if you attempt to get the content when all elements of the page have not had their chance to append HTML to the response. Thus you should invoke this method only in the application object's appendToResponse:inContext: method, and then only after super's appendToResponse:inContext: has been invoked. (For scripted applications, the appendToResponse:inContext: is implemented in Application.wos).

See Also: - setContent:, - setContentEncoding:


contentEncoding

- (NSStringEncoding)contentEncoding

Returns an integer representing the encoding used for the response content. See Content Encodings in the class description for a mapped list of supported encodings and their Objective-C names. Usually, you will want the response encoding to be the same as that used by the submitting form on the client browser. In this case it is preferable to use WORequest's formValueEncoding:

    NSStringEncoding theEncoding = [[aContext request] formValueEncoding];

The default string encoding is ISO Latin1.

See Also: - setContent:, - setContentEncoding:


headerForKey:

- (NSString *)headerForKey:(NSString *)aKey

Returns the HTTP header information identified by aKey. If there are multiple headers associated with the one key, only the first one is returned. Returns nil if there are no headers for the key.

See Also: - setHeader:forKey:


headerKeys

- (NSArray *)headerKeys

Returns an array of string keys associated with the receiver's HTTP headers. Returns nil if there are no headers. You could easily test to see if a header is included by doing something similar to this:

    NSArray *hKeys =  [aResponse headerKeys];

    if ([hKeys containsObject:@"expires"]) {

        // do something

    }

See Also: - setHeaders:forKey:


headersForKey:

- (NSArray *)headersForKey:(NSString *)aKey

Returns all HTTP headers identified by aKey.

See Also: - setHeaders:forKey:


httpVersion

- (NSString *)httpVersion

Returns the version of HTTP used for the response (for example, "HTTP/1.0").

See Also: - setHTTPVersion:


init

- (id)init

Initializes a WOResponse instance. HTTP status is set to 200 (OK), client caching is enabled, and the default string encoding is made ISO Latin 1.


setContent:

- (void)setContent:(NSData *)someData

Sets the HTML content of the HTTP response to someData.

See Also: - content


setContentEncoding:

- (void)setContentEncoding:(NSStringEncoding)anEncoding

Sets the encoding used for the content of the HTTP response. See Content Encodings in the class description for a mapped list of supported encodings and their Objective-C names. The default string encoding is ISO Latin1.

See Also: - contentEncoding


setHeader:forKey:

- (void)setHeader:(NSString *)aHeader forKey:(NSString *)aKey

Sets the HTTP header aHeader in the receiver and associates, for retrieval, the HTTP key aKey with the header. This method is commonly used to set the type of content in a response, for example:

    aResponse.setHeader("text/html", "content-type");

See Also: - headerForKey:


setHeaders:forKey:

- (void)setHeaders:(NSArray *)headerList forKey:(NSString *)aKey

Sets the list of HTTP headers headerList in the receiver and associates, for retrieval, the HTTP key aKey with the list of header elements.

See Also: - headerKeys, - headersForKey:


setHTTPVersion:

- (void)setHTTPVersion:(NSString *)aVersion

Sets the version of HTTP used for the response (for example, "HTTP/1.0").

See Also: - httpVersion


setStatus:

- (void)setStatus:(unsigned int)anInt

Sets the HTTP status to anInt. Consult the HTTP specification or HTTP documentation for the significance of status integers.

See Also: - status


setUserInfo:

- (void)setUserInfo:(NSDictionary *)aDictionary

Sets a dictionary in the WOResponse object that, as a convenience, can contain any kind of information related to the current response. Objects further down the appendToResponse:inContext: message "chain" can retrieve this information using userInfo.


status

- (unsigned int)status

Returns an integer code representing the HTTP status. Consult the HTTP specification or HTTP documentation for the significance of these status codes. By default, the status is 200 ("OK" status).

See Also: - setStatus:


userInfo

- (NSDictionary *)userInfo

Returns a dictionary that, as a convenience, can contain any kind of information related to the current response. An object further "upstream" in the appendToResponse:inContext: message "chain" can set this dictionary in the Response object as a way to pass information to other objects.

See Also: - setUserInfo: