001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.hmux;
030:
031: import com.caucho.server.connection.AbstractHttpRequest;
032: import com.caucho.server.connection.AbstractHttpResponse;
033: import com.caucho.server.util.CauchoSystem;
034: import com.caucho.util.Alarm;
035: import com.caucho.util.CharBuffer;
036: import com.caucho.vfs.WriteStream;
037:
038: import javax.servlet.http.Cookie;
039: import java.io.IOException;
040:
041: /**
042: * Handles a response for a srun connection, i.e. a connection to
043: * a web server plugin.
044: */
045: public class HmuxResponse extends AbstractHttpResponse {
046: HmuxRequest _req;
047:
048: HmuxResponse(HmuxRequest request) {
049: super (request);
050:
051: _req = request;
052: }
053:
054: /**
055: * Return true for the top request.
056: */
057: public boolean isTop() {
058: if (!(_request instanceof AbstractHttpRequest))
059: return false;
060: else {
061: return ((AbstractHttpRequest) _request).isTop();
062: }
063: }
064:
065: protected boolean writeHeadersInt(WriteStream os, int length)
066: throws IOException {
067: CharBuffer cb = _cb;
068: cb.clear();
069: cb.append((char) ((_statusCode / 100) % 10 + '0'));
070: cb.append((char) ((_statusCode / 10) % 10 + '0'));
071: cb.append((char) (_statusCode % 10 + '0'));
072: cb.append(' ');
073: cb.append(_statusMessage);
074:
075: _req.writeStatus(cb);
076:
077: if (_statusCode >= 400) {
078: removeHeader("ETag");
079: removeHeader("Last-Modified");
080: } else if (_isNoCache) {
081: removeHeader("ETag");
082: removeHeader("Last-Modified");
083:
084: setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
085: _req.writeHeader("Cache-Control", "no-cache");
086: } else if (isPrivateCache())
087: _req.writeHeader("Cache-Control", "private");
088:
089: int load = (int) (1000 * CauchoSystem.getLoadAvg());
090: _req.writeString(HmuxRequest.HMUX_META_HEADER, "cpu-load");
091: _req.writeString(HmuxRequest.HMUX_STRING, String.valueOf(load));
092:
093: for (int i = 0; i < _headerKeys.size(); i++) {
094: String key = (String) _headerKeys.get(i);
095: String value = (String) _headerValues.get(i);
096:
097: _req.writeHeader(key, value);
098: }
099:
100: if (_contentLength >= 0) {
101: cb.clear();
102: cb.append(_contentLength);
103: _req.writeHeader("Content-Length", cb);
104: } else if (length >= 0) {
105: cb.clear();
106: cb.append(length);
107: _req.writeHeader("Content-Length", cb);
108: }
109:
110: long now = Alarm.getCurrentTime();
111: for (int i = 0; i < _cookiesOut.size(); i++) {
112: Cookie cookie = (Cookie) _cookiesOut.get(i);
113: int cookieVersion = cookie.getVersion();
114:
115: fillCookie(cb, cookie, now, 0, false);
116: _req.writeHeader("Set-Cookie", cb);
117: if (cookieVersion > 0) {
118: fillCookie(cb, cookie, now, cookieVersion, true);
119: _req.writeHeader("Set-Cookie2", cb);
120: }
121: }
122:
123: if (_contentType != null) {
124: if (_charEncoding != null)
125: _req.writeHeader("Content-Type", _contentType
126: + "; charset=" + _charEncoding);
127: else
128: _req.writeHeader("Content-Type", _contentType);
129:
130: }
131:
132: _req.sendHeader();
133:
134: return false;
135: }
136: }
|