コード例 #1
0
ファイル: fetch.cpp プロジェクト: BackupTheBerlios/sim-im-svn
bool FetchClientPrivate::error_state(const char *err, unsigned)
{
    if (m_state == None)
        return false;
    if (m_state == Redirect){
        if (m_socket){
            delete m_socket;
            m_socket = NULL;
        }
        m_code = 0;
        m_hIn  = "";
        m_state = None;
        _fetch();
        return false;
    }
    if ((m_state != Done) && ((m_state != Data) || (m_size != UNKNOWN_SIZE))){
        m_code = 0;
        log(L_DEBUG, "HTTP: %s", err);
    }
    m_bDone = true;
    m_state = None;
    if (m_socket)
        m_socket->close();
    return m_client->done(m_code, m_data, m_hIn.c_str());
}
コード例 #2
0
ファイル: HTTPClient.hpp プロジェクト: rayfill/cpplib
	void close()
	{
		currentConnectionServer = "";
		currentConnectionPort = 0;
		proxyServerName = "";
		proxyServerPort = 0;
		socket.close();
		property = HTTPProperty();
	}
コード例 #3
0
ファイル: DVDStream.cpp プロジェクト: afester/CodeSamples
int DVDRemoteApp::run() {
   if (argv.size() != 2) {
       usage();
   }
   source = argv[1];

   dvdReader.setProgressListener(this);

   ServerSocket server(1234);

   // Server's main loop.
   while(1) {
      client = server.accept();
      string cmd;
      do {
         cmd = client.readLine();
         cerr << "  " << cmd << endl;
         if (cmd == "START") {
            startPlay();
         } else if (cmd == "STOP") {
            stopPlay();
         } else if (cmd == "STREAM") {
            startStream();
         } else if (cmd == "\r") {
            sendHttpResponse();
         } else if (cmd.compare(0, 13, "Range: bytes=") == 0) {
            string range = cmd.substr(13);
            size_t pos = range.find('-');
            string fromStr = range.substr(0, pos);
            string toStr = range.substr(pos + 1);
            fileOffset = atol(fromStr.c_str());
            long to = atol(toStr.c_str());
            streamSize = to - fileOffset + 1;
         }
      }while(cmd != "");
      client.close();
   }

   server.close();

   return 0;
}
コード例 #4
0
ファイル: server.cpp プロジェクト: x00one/LiteServer
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;
}
コード例 #5
0
ファイル: recipient.cpp プロジェクト: sgichohi/sauron
int main(int argc, char **argv) {
    // Validate command line arguments
    if (argc < 3) {
        cerr << "Usage: server [camera address] [camera port]\n";
        cerr << "Options: [-b OR --benchmark] [-v OR --verbose] [-m OR --max INT] [-s OR --skip INT]\n";
        return 1;
    }

    // Handle options
    long max = 0;
    bool verbose = false;

    for (int i = 3; i < argc; i++) {
        string str(argv[i]);

        if (str == "-v" || str == "--verbose") verbose = true;
    }
    
    // Loop forever pulling sendables
    for (; ;) {
        // Initiate a connection
        if (verbose) cerr << "Initiating connection...\n";
        ClientSocket *s = new ClientSocket(argv[1], argv[2]);
        
        // If the connection can't be set up, retry once every second
        while (!s->isOpen()) {
            delete s;
            s = new ClientSocket(argv[1], argv[2]);
	        this_thread::sleep_for(chrono::milliseconds(100));
        }
        
        // Get the camera's id number
        char id;
        s->recv(&id, 1);
        if (verbose) cerr << "id: " << (int)id << "\n";
        
        // Create a directory if it doesn't already exist
        stringstream sss;
        sss  << id;
        mkdir(sss.str().c_str(), 0755);
        
        // Count sent sendables
        int sent = 0;

        // Receive paths
        while(s->isOpen()) {
            // Declare variables
            char c = 0;
            string str = "";
            
            // Receive            
            try {
                s->recv(&c, 1);
                
                while (c != 0) {
                    str += c;
                    s->recv(&c, 1);
                }
            } catch (...) { break; }
            
            cout << str << "\n";
            
            sent++;
            
            // Exit if the maximum number of sendables has been hit
            if (max != 0 && sent == max) {
                s->close();
                exit(0);
            }
        }
    }
}
コード例 #6
0
ファイル: fetch.cpp プロジェクト: BackupTheBerlios/sim-im-svn
void FetchClientPrivate::packet_ready()
{
    if (m_socket->readBuffer.readPos() == m_socket->readBuffer.writePos())
        return;
    for (;;){
        if (m_state == Data){
            unsigned size = m_socket->readBuffer.writePos() - m_socket->readBuffer.readPos();
            if (size){
                if (!m_client->write_data(m_socket->readBuffer.data(m_socket->readBuffer.readPos()), size)){
                    m_socket->error_state("Write error");
                    return;
                }
            }
            m_received += size;
            if (m_received >= m_size){
                m_state = Done;
                m_socket->error_state("");
                return;
            }
            m_socket->readBuffer.init(0);
            m_socket->readBuffer.packetStart();
            return;
        }
        log_packet(m_socket->readBuffer, false, HTTPPacket);
        string line;
        string opt;
        if (!read_line(line)){
            m_socket->readBuffer.init(0);
            m_socket->readBuffer.packetStart();
            return;
        }
        switch (m_state){
        case None:
#ifdef USE_OPENSSL
        case SSLConnect:
#endif
            if (getToken(line, ' ').substr(0, 5) != "HTTP/"){
                m_socket->error_state("Bad HTTP answer");
                return;
            }
            m_code = atol(getToken(line, ' ').c_str());
            m_state = Header;
            break;
        case Header:
            if (line.empty()){
                m_state = Data;
                break;
            }
            m_hIn += line;
            m_hIn += '\x00';
            opt = getToken(line, ':');
            if (opt == "Content-Length"){
                const char *p;
                for (p = line.c_str(); *p; p++)
                    if (*p != ' ')
                        break;
                m_size = atol(p);
            }
            if ((opt == "Location") && m_bRedirect){
                const char *p;
                for (p = line.c_str(); *p; p++)
                    if (*p != ' ')
                        break;
                string proto;
                string host;
                string user;
                string pass;
                string uri;
                string extra;
                unsigned short port;
                if (!FetchClient::crackUrl(p, proto, host, port, user, pass, uri, extra)){
                    FetchClient::crackUrl(m_uri.c_str(), proto, host, port, user, pass, uri, extra);
                    extra = "";
                    if (*p == '/'){
                        uri = p;
                    }else{
                        int n = uri.find_last_of('/');
                        uri = uri.substr(0, n + 1);
                        uri += p;
                    }
                }
                m_uri = proto;
                m_uri += "://";
                m_uri += host;
                m_uri += ":";
                m_uri += number(port);
                m_uri += uri;
                if (!extra.empty()){
                    m_uri += "?";
                    m_uri += extra;
                }
                m_state = Redirect;
                m_socket->close();
                m_socket->error_state("");
                return;
            }
            break;
        default:
            break;
        }
    }
}