CWebSocket* CWebSocketManager::Handle(const char* data, unsigned int length, std::string &response) { if (data == NULL || length <= 0) return NULL; HttpParser header; HttpParser::status_t status = header.addBytes(data, length); switch (status) { case HttpParser::Error: case HttpParser::Incomplete: response.clear(); return NULL; case HttpParser::Done: default: break; } // There must be a "Sec-WebSocket-Version" header const char* value = header.getValue(WS_HEADER_VERSION_LC); if (value == NULL) { CLog::Log(LOGINFO, "WebSocket: missing Sec-WebSocket-Version"); CHttpResponse httpResponse(HTTP::Get, HTTP::BadRequest, HTTP::Version1_1); char *responseBuffer; int responseLength = httpResponse.Create(responseBuffer); response = std::string(responseBuffer, responseLength); return NULL; } CWebSocket *websocket = NULL; if (strncmp(value, "8", 1) == 0) websocket = new CWebSocketV8(); else if (strncmp(value, "13", 2) == 0) websocket = new CWebSocketV13(); if (websocket == NULL) { CLog::Log(LOGINFO, "WebSocket: Unsupported Sec-WebSocket-Version %s", value); CHttpResponse httpResponse(HTTP::Get, HTTP::UpgradeRequired, HTTP::Version1_1); httpResponse.AddHeader(WS_HEADER_VERSION, WS_SUPPORTED_VERSIONS); char *responseBuffer; int responseLength = httpResponse.Create(responseBuffer); response = std::string(responseBuffer, responseLength); return NULL; } if (websocket->Handshake(data, length, response)) return websocket; return NULL; }
void FQTermHttp::getLink(const QUrl& url, bool preview) { QUrl u=url; isExisting_ = false; isPreview_ = preview; previewEmitted = false; lastPercent_ = 0; if (u.isRelative() || u.scheme() == "file") { emit previewImage(cacheFileName_, false, true); emit done(this); return ; } if (QFile::exists(getPath(USER_CONFIG) + "hosts.cfg")) { config_ = new FQTermConfig(getPath(USER_CONFIG) + "hosts.cfg"); QString strTmp = config_->getItemValue("hosts", u.host().toLocal8Bit()); if (!strTmp.isEmpty()) { QString strUrl = u.toString(); strUrl.replace(QRegExp(u.host(), Qt::CaseInsensitive), strTmp); u = strUrl; } } if (!(netreply_ && netreply_->hasRawHeader("Location"))) { cacheFileName_ = QFileInfo(u.path()).fileName(); } if(netreply_){ netreply_->blockSignals(true); netreply_.take()->deleteLater(); } netreply_.reset(nam_->get(QNetworkRequest(u))); FQ_VERIFY(connect(netreply_.data(), SIGNAL(finished()), this, SLOT(httpDone()))); FQ_VERIFY(connect(netreply_.data(), SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(httpRead(qint64, qint64)))); FQ_VERIFY(connect(netreply_.data(), SIGNAL(error( QNetworkReply::NetworkError)), this, SLOT(httpError(QNetworkReply::NetworkError)))); FQ_VERIFY(connect(netreply_.data(), SIGNAL(metaDataChanged()), this, SLOT(httpResponse()))); }
void HttpGet::httpDone(bool error) { if (error) { qDebug() << "[HTTP] Error:" << qPrintable(http.errorString()) << httpResponse(); } if(!outputToBuffer) outputFile->close(); if(m_usecache && !m_cached && !error) { qDebug() << "[HTTP] creating cache file" << m_cachefile; QFile c(m_cachefile); c.open(QIODevice::ReadWrite); if(!outputToBuffer) { outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate); c.write(outputFile->readAll()); outputFile->close(); } else c.write(dataBuffer); c.close(); } // take care of concurring requests. If there is still one running, // don't emit done(). That request will call this slot again. if(http.currentId() == 0 && !http.hasPendingRequests()) emit done(error); }
void testApp::httpGet(string url) { char data[1024]; FILE *fp = popen("ps aux | grep Kinect", "r"); if (fp == NULL) { httpResponse("Error getting data\n"); } else { string s = ""; while (fgets(data, 1024, fp) != NULL) { s += string(data); } //system("/ProjectDonk/apps/3d/KinectTracker/bin/KinectTracker.app/Contents/MacOS/KinectTracker"); httpResponse(s); pclose(fp); } }
void uploadctr(char *filename, char *ip) { int ret, size, p, n, k, i, j; FILE *fd; SOCKET sd; unsigned char *buf; fd = fopen(filename, "rb"); if ( fd == NULL ) { printf("file %s not found\n", filename); return; } #ifndef __WIN32__ if (!is_regular_file(filename)) { printf("file %s is not a regular file\n", filename); fclose(fd); return; } #endif fseek(fd, 0, SEEK_END); size = ftell(fd); fseek(fd, 0, SEEK_SET); buf = malloc(size); if ( buf == NULL ) { printf("Not enough memory!\n"); fclose(fd); return; } fread(buf, size, 1, fd); // gah no validation, M4 will verify some... fclose(fd); ret = httpConnect(ip); if ( ret >= 0 ) { sd = ret; // connect socket if ( httpSend(sd, "/CARTIMG.BIN", buf, size, "upfile", "/upload.html", ip) >= 0 ) ret = httpResponse(sd); httpClose(sd); if ( ret == 200 ) printf("Upload OK!\r\n"); else printf("Upload error code. %i\r\n", ret); } else printf("Connect to %s failed\n", ip); free(buf); }
void uploadRom(char *filename, char *ip, int slot, char *slotname) { int ret, size, p, n, k, i, j; char fullpath[256]; // don't exceed this, the cpc wouldn't be able |cd it anyway FILE *fd; SOCKET sd; unsigned char *buf; fd = fopen(filename, "rb"); if ( fd == NULL ) { printf("file %s not found\n", filename); return; } fseek(fd, 0, SEEK_END); size = ftell(fd); fseek(fd, 0, SEEK_SET); buf = malloc(size); if ( buf == NULL ) { printf("Not enough memory!\n"); fclose(fd); return; } fread(buf, size, 1, fd); fclose(fd); ret = httpConnect(ip); if ( ret >= 0 ) { sd = ret; // connect socket if ( httpSendRom(sd, "rom.bin", buf, size, slot, "/roms.shtml", ip, slotname) >= 0 ) ret = httpResponse(sd); httpClose(sd); if ( ret == 200 ) printf("Upload OK!\r\n"); else printf("Upload error code. %i\r\n", ret); } else printf("Connect to %s failed\n", ip); free(buf); }
void ofxWSRequestHandler::httpRedirect(string url) { // a bit hacky but have no internet right now, so this is good for now string refr = "<html><head><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url="; refr += url + "\"></head></html>\r\n"; httpResponse("text/html", refr); }
bool CWebSocketV8::Handshake(const char* data, size_t length, std::string &response) { std::string strHeader(data, length); const char *value; HttpParser header; if (header.addBytes(data, length) != HttpParser::Done) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: incomplete handshake received"); return false; } // The request must be GET value = header.getMethod(); if (value == NULL || strnicmp(value, WS_HTTP_METHOD, strlen(WS_HTTP_METHOD)) != 0) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid HTTP method received (GET expected)"); return false; } // The request must be HTTP/1.1 or higher size_t pos; if ((pos = strHeader.find(WS_HTTP_TAG)) == std::string::npos) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid handshake received"); return false; } pos += strlen(WS_HTTP_TAG); std::istringstream converter(strHeader.substr(pos, strHeader.find_first_of(" \r\n\t", pos) - pos)); float fVersion; converter >> fVersion; if (fVersion < 1.1f) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid HTTP version %f (1.1 or higher expected)", fVersion); return false; } std::string websocketKey, websocketProtocol; // There must be a "Host" header value = header.getValue("host"); if (value == NULL || strlen(value) == 0) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: \"Host\" header missing"); return true; } // There must be a base64 encoded 16 byte (=> 24 byte as base64) "Sec-WebSocket-Key" header value = header.getValue(WS_HEADER_KEY_LC); if (value == NULL || (websocketKey = value).size() != 24) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid \"Sec-WebSocket-Key\" received"); return true; } // There might be a "Sec-WebSocket-Protocol" header value = header.getValue(WS_HEADER_PROTOCOL_LC); if (value && strlen(value) > 0) { std::vector<std::string> protocols = StringUtils::Split(value, ","); for (std::vector<std::string>::iterator protocol = protocols.begin(); protocol != protocols.end(); ++protocol) { StringUtils::Trim(*protocol); if (*protocol == WS_PROTOCOL_JSONRPC) { websocketProtocol = WS_PROTOCOL_JSONRPC; break; } } } CHttpResponse httpResponse(HTTP::Get, HTTP::SwitchingProtocols, HTTP::Version1_1); httpResponse.AddHeader(WS_HEADER_UPGRADE, WS_HEADER_UPGRADE_VALUE); httpResponse.AddHeader(WS_HEADER_CONNECTION, WS_HEADER_UPGRADE); httpResponse.AddHeader(WS_HEADER_ACCEPT, calculateKey(websocketKey)); if (!websocketProtocol.empty()) httpResponse.AddHeader(WS_HEADER_PROTOCOL, websocketProtocol); char *responseBuffer; int responseLength = httpResponse.Create(responseBuffer); response = std::string(responseBuffer, responseLength); m_state = WebSocketStateConnected; return true; }
void upload(char *filename, char *path, char *ip, int opt, unsigned short start, unsigned short exec) { int ret, size, p, n, k, i, j; char fullpath[256]; // don't exceed this, the cpc wouldn't be able |cd it anyway FILE *fd; SOCKET sd; unsigned char *buf; fd = fopen(filename, "rb"); if ( fd == NULL ) { printf("file %s not found\n", filename); return; } #ifndef __WIN32__ if (!is_regular_file(filename)) { printf("file %s is not a regular file\n", filename); fclose(fd); return; } #endif fseek(fd, 0, SEEK_END); size = ftell(fd); fseek(fd, 0, SEEK_SET); buf = malloc(size+0x80); if ( buf == NULL ) { printf("Not enough memory!\n"); fclose(fd); return; } if ( opt != 0 ) { _cpchead *cpcheader = (_cpchead *)buf; memset(cpcheader, 0, 0x80); // set up cleaned filename and extionsion formatfn(cpcheader->filename, filename); getExtension(cpcheader->extension, filename); // add more file types here... and more paramters.... switch (opt) { case 1: cpcheader->addr = 0x172; // protext uses this cpcheader->type = 10; cpcheader->size = size; cpcheader->size2 = size; cpcheader->checksum = checksum16(buf, 66); break; default: case 2: // binary header cpcheader->addr = start; cpcheader->exec = exec; cpcheader->type = 2; cpcheader->size = size; cpcheader->size2 = size; cpcheader->checksum = checksum16(buf, 66); break; } fread(&buf[0x80], size, 1, fd); size+=0x80; } else fread(buf, size, 1, fd); fclose(fd); ret = httpConnect(ip); if ( ret >= 0 ) { sd = ret; // connect socket p = pathPos(filename, strlen(filename)); // strip any PC path from filename sprintf(fullpath, "%s/%s", path, &filename[p]); k = strlen(fullpath); n = 0; while ( n < k ) // remove leading / from path { if ( fullpath[n] != '/' ) break; n++; } // remove duplicate /'s k = strlen(fullpath); j = 0; for (i=n; i < (k+1); i++) { if ( (fullpath[i] == '/') && (fullpath[i+1] == '/') ) i++; fullpath[j++] = fullpath[i]; } if ( httpSend(sd, fullpath, buf, size, "upfile", "/upload.html", ip) >= 0 ) ret = httpResponse(sd); httpClose(sd); if ( ret == 200 ) printf("Upload OK!\r\n"); else printf("Upload error code. %i\r\n", ret); } else printf("Connect to %s failed\n", ip); free(buf); }