Exemplo n.º 1
0
ProjImageIn ProjImageIn::createFromSocket( ClientSocket & socket )
{
    unsigned int strLen(0);
    char * pszParamfilename = NULL;
    std::string strParamfilename("");
    int i = 0; 
    
    socket.receive(&i, sizeof(i));
    // in this case what is trying to be constructed here is a list, and 
    // not an individual image. 
    if ( i != -1 )
    {
        WRITE_DEBUG ( "i = " << i << std::endl; )
        throw GeneralException("Error, not an individual image in transit.");
Exemplo n.º 2
0
void* clientThread (void* arg) {
    ClientSocket* client = (ClientSocket*)arg;
    client->setReceiveTimeout(5);

    char buffer[(1024 * 4) + 1];
    ssize_t size = client->receive(buffer, 1024 * 4);
    buffer[size] = 0;
    
    if (size > 0) {
        HttpRequest* request = new HttpRequest(buffer, (size_t)size);
        HttpResponse* response = new HttpResponse();

        if ((request->uri != NULL) && (request->method != NULL)) {
            std::string path = documentRoot;
            path += request->uri;

            struct stat fileInfo;
            int statRes = stat(path.c_str(), &fileInfo);

            if ((statRes == -1) || ((fileInfo.st_mode & S_IFMT) != S_IFREG)) {
                response->status = "404";
                response->reason = "Not found";
                response->contentLength = 9;

                response->headers.push_back("Content-Type: text/html");

                std::string answer = response->compileHeader();
                client->send(answer.c_str(), answer.length());
                client->send("Not found", response->contentLength);
            } else {
                response->status = "200";
                response->reason = "OK";

                // Get content-type.
                const char * contentType = mineMapping.parsePath(path.c_str());

                if (contentType != NULL) {
                    std::string contentTypeHeader = "Content-Type: ";
                    contentTypeHeader += contentType;
                    response->headers.push_back(contentTypeHeader.c_str());

                    response->contentLength = fileInfo.st_size;

                    std::string answer = response->compileHeader();
                    client->send(answer.c_str(), answer.length());
                    client->sendFile(path.c_str(), fileInfo.st_size);
                } else {
                    FcgiBridge* fcgi = new FcgiBridge(client, response);
                    fcgi->query(request->method, path.c_str(), request->queryString, request->headerDataBlock);
                    delete fcgi;
                }
            }
        }

        delete request;
        delete response;
    }
    
    client->close();
    delete client;
    
    return NULL;
}
void FileTransferSocketThread::execute()
{
    if(info.hostType == eServer)
    {
        ServerSocket serverSocket;
        serverSocket.bind(this->info.serverPort);
        serverSocket.listen(1);
        Socket *clientSocket = serverSocket.accept();

        char data[513]="";
        memset(data, 0, 256);

        clientSocket->receive(data,256, true);
        if(*data == SEND_FILE)
        {
            FileInfo file;

            memcpy(&file, data+1, sizeof(file));

            *data=ACK;
            clientSocket->send(data,256);

            Checksum checksum;
            checksum.addFile(file.fileName);
            file.filecrc  = checksum.getSum();

            ifstream infile(file.fileName.c_str(), ios::in | ios::binary | ios::ate);
            if(infile.is_open() == true)
            {
                file.filesize = infile.tellg();
                infile.seekg (0, ios::beg);

                memset(data, 0, 256);
                *data=SEND_FILE;
                memcpy(data+1,&file,sizeof(file));

                clientSocket->send(data,256);
                clientSocket->receive(data,256, true);
                if(*data != ACK) {
                   //transfer error
                }

                int remain=file.filesize % 512 ;
                int packs=(file.filesize-remain)/512;

                while(packs--)
                {
                    infile.read(data,512);
                    //if(!ReadFile(file,data,512,&read,NULL))
                    //    ; //read error
                    //if(written!=pack)
                    //    ; //read error
                    clientSocket->send(data,512);
                    clientSocket->receive(data,256, true);
                    if(*data!=ACK) {
                           //transfer error
                    }
                }

                infile.read(data,remain);
                //if(!ReadFile(file,data,remain,&read,NULL))
                //   ; //read error
                //if(written!=pack)
                //   ; //read error

                clientSocket->send(data,remain);
                clientSocket->receive(data,256, true);
                if(*data!=ACK) {
                   //transfer error
                }

                infile.close();
            }
        }

        delete clientSocket;
    }
    else
    {
        Ip ip(this->info.serverIP);
        ClientSocket clientSocket;
        clientSocket.connect(this->info.serverIP, this->info.serverPort);

        if(clientSocket.isConnected() == true)
        {
            FileInfo file;
            file.fileName = this->info.fileName;
            //file.filesize =
            //file.filecrc  = this->info.

            string path = extractDirectoryPathFromFile(file.fileName);
            createDirectoryPaths(path);
            ofstream outFile(file.fileName.c_str(), ios_base::binary | ios_base::out);
            if(outFile.is_open() == true)
            {
                char data[513]="";
                memset(data, 0, 256);
                *data=SEND_FILE;
                memcpy(data+1,&file,sizeof(file));

                clientSocket.send(data,256);
                clientSocket.receive(data,256, true);
                if(*data!=ACK) {
                  //transfer error
                }

                clientSocket.receive(data,256,true);
                if(*data == SEND_FILE)
                {
                   memcpy(&file, data+1, sizeof(file));
                   *data=ACK;
                   clientSocket.send(data,256);

                   int remain = file.filesize % 512 ;
                   int packs  = (file.filesize-remain) / 512;

                   while(packs--)
                   {
                      clientSocket.receive(data,512,true);

                      outFile.write(data, 512);
                      if(outFile.bad())
                      {
                          //int ii = 0;
                      }
                      //if(!WriteFile(file,data,512,&written,NULL))
                      //   ; //write error
                      //if(written != pack)
                      //   ; //write error
                      *data=ACK;
                      clientSocket.send(data,256);
                    }
                    clientSocket.receive(data,remain,true);

                    outFile.write(data, remain);
                    if(outFile.bad())
                    {
                        //int ii = 0;
                    }

                    //if(!WriteFile(file,data,remain,&written,NULL))
                    //    ; //write error
                    //if(written!=pack)
                    //    ; //write error
                    *data=ACK;
                    clientSocket.send(data,256);

                    Checksum checksum;
                    checksum.addFile(file.fileName);
                    uint32 crc = checksum.getSum();
                    if(file.filecrc != crc)
                    {
                        //int ii = 0;
                    }

                    //if(calc_crc(file)!=info.crc)
                    //   ; //transfeer error
                }

                outFile.close();
            }
        }
    }
}