00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "HTTPSink.hh"
00022 #include "GroupsockHelper.hh"
00023
00024 #include <string.h>
00025 #if defined(__WIN32__) || defined(_WIN32)
00026 #define snprintf _snprintf
00027 #endif
00028
00030
00031 HTTPSink* HTTPSink::createNew(UsageEnvironment& env, Port ourPort) {
00032 int ourSocket = -1;
00033
00034 do {
00035 int ourSocket = setUpOurSocket(env, ourPort);
00036 if (ourSocket == -1) break;
00037
00038 HTTPSink* newSink = new HTTPSink(env, ourSocket);
00039 if (newSink == NULL) break;
00040
00041 appendPortNum(env, ourPort);
00042
00043 return newSink;
00044 } while (0);
00045
00046 if (ourSocket != -1) ::closeSocket(ourSocket);
00047 return NULL;
00048 }
00049
00050 int HTTPSink::setUpOurSocket(UsageEnvironment& env, Port& ourPort) {
00051 int ourSocket = -1;
00052
00053 do {
00054 ourSocket = setupStreamSocket(env, ourPort);
00055 if (ourSocket < 0) break;
00056
00057
00058 if (!increaseSendBufferTo(env, ourSocket, 50*1024)) break;
00059
00060 if (listen(ourSocket, 1) < 0) {
00061 env.setResultErrMsg("listen() failed: ");
00062 break;
00063 }
00064
00065 if (ourPort.num() == 0) {
00066
00067 if (!getSourcePort(env, ourSocket, ourPort)) break;
00068 }
00069
00070 return ourSocket;
00071 } while (0);
00072
00073 if (ourSocket != -1) ::closeSocket(ourSocket);
00074 return -1;
00075 }
00076
00077 void HTTPSink::appendPortNum(UsageEnvironment& env,
00078 Port const& port) {
00079 char tmpBuf[10];
00080 sprintf(tmpBuf, " %d", ntohs(port.num()));
00081 env.appendToResultMsg(tmpBuf);
00082 }
00083
00084
00085 HTTPSink::HTTPSink(UsageEnvironment& env, int ourSocket)
00086 : MediaSink(env), fSocket(ourSocket), fClientSocket(-1) {
00087 }
00088
00089 HTTPSink::~HTTPSink() {
00090 ::closeSocket(fSocket);
00091 }
00092
00093 Boolean HTTPSink::isUseableFrame(unsigned char* ,
00094 unsigned ) {
00095
00096 return True;
00097 }
00098
00099 Boolean HTTPSink::continuePlaying() {
00100 if (fSource == NULL) return False;
00101
00102 if (fClientSocket < 0) {
00103
00104
00105 struct sockaddr_in clientAddr;
00106 SOCKLEN_T clientAddrLen = sizeof clientAddr;
00107 fClientSocket = accept(fSocket, (struct sockaddr*)&clientAddr,
00108 &clientAddrLen);
00109 if (fClientSocket < 0) {
00110 int err = envir().getErrno();
00111 if (err != EWOULDBLOCK) {
00112 envir().setResultErrMsg("accept() failed: ");
00113 return False;
00114 }
00115 } else {
00116
00117
00118
00119 char okResponse[400];
00120 const char* responseFmt = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Length: 2147483647\r\nContent-Type: %s\r\n\r\n";
00121 #if defined(IRIX) || defined(ALPHA) || defined(_QNX4) || defined(IMN_PIM) || defined(CRIS) || defined (VXWORKS)
00122
00123 sprintf(okResponse, responseFmt, fSource->MIMEtype());
00124 #else
00125 snprintf(okResponse, sizeof okResponse, responseFmt, fSource->MIMEtype());
00126 #endif
00127 send(fClientSocket, okResponse, strlen(okResponse), 0);
00128 }
00129 }
00130
00131 fSource->getNextFrame(fBuffer, sizeof fBuffer,
00132 afterGettingFrame, this,
00133 ourOnSourceClosure, this);
00134
00135 return True;
00136 }
00137
00138 void HTTPSink::ourOnSourceClosure(void* clientData) {
00139
00140 HTTPSink* sink = (HTTPSink*) clientData;
00141 ::closeSocket(sink->fClientSocket);
00142 sink->fClientSocket = -1;
00143 onSourceClosure(sink);
00144 }
00145
00146 void HTTPSink::afterGettingFrame(void* clientData, unsigned frameSize,
00147 unsigned ,
00148 struct timeval presentationTime,
00149 unsigned ) {
00150 HTTPSink* sink = (HTTPSink*)clientData;
00151 sink->afterGettingFrame1(frameSize, presentationTime);
00152 }
00153
00154 void HTTPSink::afterGettingFrame1(unsigned frameSize,
00155 struct timeval ) {
00156
00157 if (fClientSocket >= 0 && isUseableFrame(fBuffer, frameSize)) {
00158 int sendResult
00159 = send(fClientSocket, (char*)(&fBuffer[0]), frameSize, 0);
00160 if (sendResult < 0) {
00161 int err = envir().getErrno();
00162 if (err != EWOULDBLOCK) {
00163
00164
00165 ourOnSourceClosure(this);
00166 return;
00167 }
00168 }
00169 }
00170
00171
00172 continuePlaying();
00173 }