int sseServer::HandleResponse(int iCon) { int streamflag = 0; char imesg[99999]; char mesg[99999], *reqline[3], data_to_send[4096], path[99999]; int fd, bytes_read; int rcvd; rcvd = recvfrom(connfd[iCon],imesg,99999,0,(struct sockaddr *)&(cliaddr[iCon]),&(clilen[iCon])); if (rcvd<0) // receive error fprintf(stderr,("recv() error\n")); else if (rcvd==0) // receive socket closed fprintf(stderr,"Client disconnected upexpectedly.\n"); else { std::string sendstuff = ""; std::string header = ""; std::cout << "Recieved:\n" << imesg << std::endl; reqline[0] = strtok (imesg, " \t\n"); if ( strncmp(reqline[0], "GET\0", 4)==0 ) { reqline[1] = strtok (NULL, " \t"); reqline[2] = strtok (NULL, " \t\n"); if ( strncmp( reqline[2], "HTTP/1.0", 8)!=0 && strncmp( reqline[2], "HTTP/1.1", 8)!=0 ) { write(connfd[iCon], "HTTP/1.1 400 Bad Request\n", 25); } else { if ( strncmp(reqline[1], "/\0", 2)==0 ) { reqline[1] = "/src/Util/webui/index.html"; } std::string req = reqline[1]; // Check to see if a stream event is being requested if((req.find("/stream")) != std::string::npos) { // A stream request has been made, we need to continously send to // the stream in a new thread. strconn_data* data = new strconn_data(); streamflag = 1; pthread_t* thread = new pthread_t; data->server = this; data->pid = thread; data->iCon = connfd+iCon; data->slot = iCon; int rc = pthread_create(thread,NULL,stream_connection,(void*)(data)); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } pthread_exit(0); } else { strcpy(path, ROOT); strcpy(&path[strlen(ROOT)], reqline[1]); printf("file: %s\n", path); std::ifstream is (path); if ( is ) //FILE FOUND { std::string temp = ""; header = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n"; is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); char * buffer = new char [length]; is.read (buffer,length); temp = buffer; replace(temp,std::string("<!PORT_NUMBER!>"),std::string(PORTc)); sendstuff = temp + "\n\n\0"; std::cout << header + sendstuff << std::endl; } else header ="HTTP/1.1 404 Not Found\n\n"; //FILE NOT FOUND std::string temp = (header+sendstuff); io_mutex[connfd[iCon]].lock(); send(connfd[iCon], temp.c_str(), temp.length(),0); io_mutex[connfd[iCon]].unlock(); } } } else if ( strncmp(reqline[0], "POST\0", 4)==0 ) { // Process a post ProcessPost(iCon); printf("Got a Post\n %s\n %s/n",reqline[1],reqline[2]); if ( strncmp(reqline[1], "/\0", 2)==0 ) { reqline[1] = "/src/Util/webui/index.html"; } strcpy(path, ROOT); strcpy(&path[strlen(ROOT)], reqline[1]); printf("file: %s\n", path); std::ifstream is (path); if ( is ) //FILE FOUND { std::string temp = ""; header = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n"; is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); char * buffer = new char [length]; is.read (buffer,length); temp = buffer; replace(temp,std::string("<!PORT_NUMBER!>"),std::string(PORTc)); sendstuff = temp + "\n\n\0"; std::cout << header + sendstuff << std::endl; } else header ="HTTP/1.1 404 Not Found\n\n"; //FILE NOT FOUND std::string temp = (header+sendstuff); io_mutex[connfd[iCon]].lock(); send(connfd[iCon], temp.c_str(), temp.length(),0); io_mutex[connfd[iCon]].unlock(); } } shutdown (connfd[iCon], SHUT_RDWR); close(connfd[iCon]); connfd[iCon] = -1; pthread_exit(0); }
RsslRet UPAProvider::ProcessItemRequest(RsslChannel* chnl, RsslMsg* msg, RsslDecodeIterator* dIter) { RsslMsgKey* key = 0; RsslUInt8 domainType = msg->msgBase.domainType; RsslInt32 streamId = msg->msgBase.streamId; switch(msg->msgBase.msgClass) { case RSSL_MC_REQUEST: // get key { key = (RsslMsgKey *)rsslGetMsgKey(msg); bool isPrivateStream = (msg->requestMsg.flags & RSSL_RQMF_PRIVATE_STREAM) > 0 ? RSSL_TRUE : RSSL_FALSE; RMDSPublisherSource *source = owner_->GetSource(); // insert the sourcename into the new item request as we cant get it from the servioce id if ( source == 0) { if (UPAPublisherItem::SendItemRequestReject(chnl, streamId, domainType, RSSL_SC_USAGE_ERROR, "Invalid service id", isPrivateStream) != RSSL_RET_SUCCESS) return RSSL_RET_FAILURE; break; } bool isNew; UPAPublisherItem_ptr_t newPubItem = source->AddItem(chnl, streamId, source->Name(), string(key->name.data, key->name.length), key->serviceId, owner_, isNew); // now need to add this item to our channel dictionary so we can find it again when we get a close ChannelDictionaryItem_t * dictItem = 0; ChannelDictionary_t::iterator it = channelDictionary_.find(chnl); if (it == channelDictionary_.end()) { t42log_error("received item request for %s : %s on unknown channel (%d)\n", source->Name().c_str(), string(key->name.data, key->name.length).c_str(), chnl); break; } // so now we can insert the stream it in the dictionary item dictItem = it->second; dictItem->streamIdMap_[streamId] = newPubItem; // request a new item. Send a message to the client // // if its not a new item we want to request a recap, that will be send on the new channel / stream owner_->RequestItem(source->Name(), string(key->name.data, key->name.length), !isNew); } break; case RSSL_MC_CLOSE: { t42log_debug("Received Item Close for StreamId %d\n", streamId); // find the item in the channel dictionary ChannelDictionary_t::iterator it = channelDictionary_.find(chnl); if (it == channelDictionary_.end()) { t42log_error("received close item request for stream %d on unknown channel (%d)\n", streamId, chnl); break; } ChannelDictionaryItem_t * dictItem = it->second; // now look up the stream id ChannelStreamIdMap_t::iterator streamIt = dictItem->streamIdMap_.find(streamId); if (streamIt == dictItem->streamIdMap_.end()) { t42log_warn("received item close for unknown stream id %d on channel %d \n", streamId, chnl); break; } UPAPublisherItem_ptr_t item = streamIt->second; dictItem->streamIdMap_.erase(streamIt); if (!item->RemoveChannel(chnl, streamId)) { // this will send a message to the mama client to tell iut we dont want this any more owner_->CloseItem(item->Source(), item->Symbol()); } } break; case RSSL_MC_POST: if (ProcessPost(chnl, msg, dIter) != RSSL_RET_SUCCESS) return RSSL_RET_FAILURE; break; default: printf("Received Unhandled Item Msg Class: %d\n", msg->msgBase.msgClass); break; } return RSSL_RET_SUCCESS; }