00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _PASSENGER_SESSION_H_
00026 #define _PASSENGER_SESSION_H_
00027
00028 #include <sys/types.h>
00029 #include <sys/socket.h>
00030 #include <sys/un.h>
00031 #include <unistd.h>
00032 #include <netdb.h>
00033 #include <cerrno>
00034 #include <cstring>
00035
00036 #include <string>
00037 #include <vector>
00038
00039 #include <boost/shared_ptr.hpp>
00040 #include <boost/function.hpp>
00041 #include <oxt/backtrace.hpp>
00042 #include <oxt/system_calls.hpp>
00043
00044 #include "MessageChannel.h"
00045 #include "StaticString.h"
00046 #include "Exceptions.h"
00047
00048 namespace Passenger {
00049
00050 using namespace boost;
00051 using namespace oxt;
00052 using namespace std;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 class Session {
00080 protected:
00081 string detachKey;
00082 string connectPassword;
00083 string gupid;
00084
00085 public:
00086
00087
00088
00089 virtual ~Session() {}
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 virtual void initiate() = 0;
00100
00101
00102
00103
00104
00105 virtual bool initiated() const = 0;
00106
00107
00108
00109
00110
00111
00112
00113 virtual string getSocketType() const = 0;
00114
00115
00116
00117
00118
00119
00120
00121
00122 virtual string getSocketName() const = 0;
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 virtual void sendHeaders(const char *headers, unsigned int size) {
00153 TRACE_POINT();
00154 int stream = getStream();
00155 if (stream == -1) {
00156 throw IOException("Cannot write headers to the request handler "
00157 "because the I/O stream has already been closed or discarded.");
00158 }
00159 try {
00160 MessageChannel(stream).writeScalar(headers, size);
00161 } catch (SystemException &e) {
00162 e.setBriefMessage("An error occured while writing headers "
00163 "to the request handler");
00164 throw;
00165 }
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 virtual void sendHeaders(const StaticString &headers) {
00177 sendHeaders(headers.c_str(), headers.size());
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 virtual void sendBodyBlock(const char *block, unsigned int size) {
00196 TRACE_POINT();
00197 int stream = getStream();
00198 if (stream == -1) {
00199 throw IOException("Cannot write request body block to the "
00200 "request handler because the I/O channel has "
00201 "already been closed or discarded.");
00202 }
00203 try {
00204 MessageChannel(stream).writeRaw(block, size);
00205 } catch (SystemException &e) {
00206 e.setBriefMessage("An error occured while sending the "
00207 "request body to the request handler");
00208 throw;
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 virtual int getStream() const = 0;
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 virtual void setReaderTimeout(unsigned int msec) = 0;
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 virtual void setWriterTimeout(unsigned int msec) = 0;
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 virtual void shutdownReader() = 0;
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 virtual void shutdownWriter() = 0;
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 virtual void closeStream() = 0;
00280
00281
00282
00283
00284
00285
00286
00287
00288 virtual void discardStream() = 0;
00289
00290
00291
00292
00293
00294 virtual pid_t getPid() const = 0;
00295
00296 const string getDetachKey() const {
00297 return detachKey;
00298 }
00299
00300
00301
00302
00303
00304 const string getConnectPassword() const {
00305 return connectPassword;
00306 }
00307
00308 const string getGupid() const {
00309 return gupid;
00310 }
00311 };
00312
00313 typedef shared_ptr<Session> SessionPtr;
00314
00315
00316
00317
00318
00319 class StandardSession: public Session {
00320 public:
00321 typedef function<void (const StandardSession *)> CloseCallback;
00322
00323 protected:
00324 pid_t pid;
00325 CloseCallback closeCallback;
00326 string socketType;
00327 string socketName;
00328
00329
00330 int fd;
00331 bool isInitiated;
00332
00333 public:
00334 StandardSession(pid_t pid,
00335 const CloseCallback &closeCallback,
00336 const string &socketType,
00337 const string &socketName,
00338 const string &detachKey,
00339 const string &connectPassword,
00340 const string &gupid)
00341 {
00342 TRACE_POINT();
00343 if (socketType != "unix" && socketType != "tcp") {
00344 throw IOException("Unsupported socket type '" + socketType + "'");
00345 }
00346 this->pid = pid;
00347 this->closeCallback = closeCallback;
00348 this->socketType = socketType;
00349 this->socketName = socketName;
00350 this->detachKey = detachKey;
00351 this->connectPassword = connectPassword;
00352 this->gupid = gupid;
00353 fd = -1;
00354 isInitiated = false;
00355 }
00356
00357 virtual ~StandardSession() {
00358 TRACE_POINT();
00359 closeStream();
00360 if (closeCallback != NULL) {
00361 closeCallback(this);
00362 }
00363 }
00364
00365 virtual void initiate() {
00366 TRACE_POINT();
00367 if (socketType == "unix") {
00368 fd = connectToUnixServer(socketName.c_str());
00369 } else {
00370 vector<string> args;
00371
00372 split(socketName, ':', args);
00373 if (args.size() != 2 || atoi(args[1]) == 0) {
00374 UPDATE_TRACE_POINT();
00375 throw IOException("Invalid TCP/IP address '" + socketName + "'");
00376 }
00377 fd = connectToTcpServer(args[0].c_str(), atoi(args[1]));
00378 }
00379 isInitiated = true;
00380 }
00381
00382 virtual bool initiated() const {
00383 return isInitiated;
00384 }
00385
00386 virtual string getSocketType() const {
00387 return socketType;
00388 }
00389
00390 virtual string getSocketName() const {
00391 return socketName;
00392 }
00393
00394 virtual int getStream() const {
00395 return fd;
00396 }
00397
00398 virtual void setReaderTimeout(unsigned int msec) {
00399 MessageChannel(fd).setReadTimeout(msec);
00400 }
00401
00402 virtual void setWriterTimeout(unsigned int msec) {
00403 MessageChannel(fd).setWriteTimeout(msec);
00404 }
00405
00406 virtual void shutdownReader() {
00407 TRACE_POINT();
00408 if (fd != -1) {
00409 int ret = syscalls::shutdown(fd, SHUT_RD);
00410 if (ret == -1) {
00411 int e = errno;
00412
00413 if (e != ENOTCONN) {
00414 throw SystemException("Cannot shutdown the reader stream",
00415 e);
00416 }
00417 }
00418 }
00419 }
00420
00421 virtual void shutdownWriter() {
00422 TRACE_POINT();
00423 if (fd != -1) {
00424 int ret = syscalls::shutdown(fd, SHUT_WR);
00425 if (ret == -1) {
00426 int e = errno;
00427
00428 if (e != ENOTCONN) {
00429 throw SystemException("Cannot shutdown the writer stream",
00430 e);
00431 }
00432 }
00433 }
00434 }
00435
00436 virtual void closeStream() {
00437 TRACE_POINT();
00438 if (fd != -1) {
00439 int ret = syscalls::close(fd);
00440 fd = -1;
00441 if (ret == -1) {
00442 int e = errno;
00443 if (errno == EIO) {
00444 throw SystemException(
00445 "A write operation on the session stream failed",
00446 e);
00447 } else {
00448 throw SystemException(
00449 "Cannot close the session stream",
00450 e);
00451 }
00452 }
00453 }
00454 }
00455
00456 virtual void discardStream() {
00457 fd = -1;
00458 }
00459
00460 virtual pid_t getPid() const {
00461 return pid;
00462 }
00463 };
00464
00465
00466 }
00467
00468 #endif