RssReader::RssReader(QWidget *parent) : QWidget(parent), ui(new Ui::RssReader) { ui->setupUi(this); QTreeWidgetItem *root1 = new QTreeWidgetItem(this->ui->treeWidget_Rsslist,QStringList(QString(QObject::tr("科技")))); QTreeWidgetItem *root2 = new QTreeWidgetItem(this->ui->treeWidget_Rsslist,QStringList(QString(QObject::tr("社会")))); QTreeWidgetItem *root3 = new QTreeWidgetItem(this->ui->treeWidget_Rsslist,QStringList(QString(QObject::tr("生活")))); QTreeWidgetItem *leaf1 = new QTreeWidgetItem(root1,QStringList(QString("cnBeta"))); QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root1,QStringList(QString("新浪焦点新闻"))); QTreeWidgetItem *leaf3 = new QTreeWidgetItem(root3,QStringList(QString("糗事百科"))); ui->webView_Rss->setHtml("<body style=\"background-color:rgb(222,222,222);font-family:微软雅黑\"></body>"); //ui->webView_Passage->setHtml("<body style=\"background-color:rgb(222,222,222);font-family:微软雅黑\"></body>"); pRssObject = new RssObject(this); pGetRss = new QNetworkAccessManager(this); pGetPassage = new QNetworkAccessManager(this); connect(pGetRss,SIGNAL(finished(QNetworkReply*)),this,SLOT(RssFinished(QNetworkReply*))); connect(pGetPassage,SIGNAL(finished(QNetworkReply*)),this,SLOT(PassageFinished(QNetworkReply*))); connect(ui->webView_Rss->page()->mainFrame(),SIGNAL(javaScriptWindowObjectCleared()),this,SLOT(addJavaScriptObject())); connect(pRssObject,SIGNAL(sendHTML(QString)),this,SLOT(readUrl(QString))); connect(ui->treeWidget_Rsslist,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(ChoseRss(QTreeWidgetItem*))); ui->webView_Passage->hide(); }
void SC_Web_Server::sendPage(const char *path) { sendHTML( "<html>\n" "<head><title>File Upload</title></head>\n" "<body>\n" "<h2>Upload File</h2>\n" "<form method=\"post\" enctype=\"multipart/form-data\" action=\"upload\">\n" "Choose the file to upload:\n" "<input type=\"file\" name=\"fileID\" /><br />\n" "<input type=\"submit\" value=\"Send\" />\n" "</form>\n" "</body>\n" "</html>\n"); /* <form action="upload.php" method="post"> <input type="file" onchange="this.form.submit()" name="myFile"/> </form> - or - document.getElementById('myfilefield').onchange = function() { this.form.submit(); }; */ }
char SC_Web_Server::on_receive() { Flio_TCP_Socket::on_receive(); if (!available()) return 1; if (pState==WEB_IDLE) { // start receiving a client request pBufferPos = recv(pBuffer, 4096); pBuffer[pBufferPos] = 0; if (strncmp(pBuffer, "GET ", 4)==0) { // client requests some document pState = WEB_CMD_GET; } else if (strncmp(pBuffer, "POST ", 5)==0) { // client sends a file pState = WEB_CMD_POST; } else { // FIXME: Zombie! } } else { // append more data if available int n = available(), max = 4096-pBufferPos; if (n>max) n = max; pBufferPos += recv(pBuffer+pBufferPos, n); pBuffer[pBufferPos] = 0; } if (pState==WEB_CMD_GET || pState==WEB_CMD_POST) { // find the magic empty line (header must be less than sizeof(pBuffer) const char *emptyLine = strstr(pBuffer, "\r\n\r\n"); if (!emptyLine) emptyLine = strstr(pBuffer, "\n\n"); if (emptyLine) { int nHeader = int(emptyLine-pBuffer) + ((*emptyLine=='\r')?4:2); pBuffer[nHeader-1]=0; printf("<---- POST: \n%s\n--->\n", pBuffer); if (pState==WEB_CMD_GET) { sendPage(pBuffer+4); pState = WEB_IDLE; } else if (pState==WEB_CMD_POST) { // TODO: interprete the client request const char *cn = strstr(pBuffer, "Content-Length: "); if (cn) { pUploadRemaining = atoi(cn+16); printf("Content Length: >>%d<<\n", (int)pUploadRemaining); } else { pUploadRemaining=0; } const char *bd = strstr(pBuffer, "boundary="); if (bd) { const char *bde = strstr(bd+9, "\r\n"); if (bde) { size_t len = bde-bd-9; strcpy(pBoundary, "\r\n--"); memcpy(pBoundary+4, bd+9, len); pBoundary[len+4] = 0; printf("Boundary found: >>%s<<\n", pBoundary); strcat(pBoundary, "--\r\n"); } } pState = WEB_UPLOAD_HEADER; } // move the remaining part of the buffer to the buffer start int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; } } if (pState==WEB_UPLOAD_HEADER) { const char *emptyLine = strstr(pBuffer, "\r\n\r\n"); if (!emptyLine) emptyLine = strstr(pBuffer, "\n\n"); if (emptyLine) { int nHeader = int(emptyLine-pBuffer) + ((*emptyLine=='\r')?4:2); pBuffer[nHeader-1]=0; printf("<---- POST content: \n%s\n--->\n", pBuffer); // TODO: interprete the content header const char *fn = strstr(pBuffer, "filename=\""); if (fn) { const char *fne = strstr(fn+10, "\""); if (fne) { size_t len = fne-fn-10; memcpy(pFilename, fn+10, len); pFilename[len] = 0; printf("Filename found: >>%s<<\n", pFilename); } } // move the remaining part of the buffer to the buffer start int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; pState = WEB_UPLOAD_DATA; // TODO: open file char buf[2048]; strcpy(buf, gUploadPath); strcat(buf, pFilename); pFile = fopen(buf, "wb"); } } if (pState==WEB_UPLOAD_DATA) { pBuffer[pBufferPos] = 0; int n = pBufferPos; int nbd = (int)strlen(pBoundary); //const char *bd = strstr(pBuffer, pBoundary); const char *bd = (char*)memmem(pBuffer, pBufferPos, pBoundary, nbd); if (bd) { n = int(bd-pBuffer); //printf("End found\n"); } else if (n<nbd) { //printf("Less than bd (%d)\n", n); const char *safe = strchr(pBuffer, '\r'); if (safe) n = int(safe-pBuffer); } else { //printf("More than bd (%d)\n", n); const char *safe = strchr(pBuffer+pBufferPos-nbd, '\r'); //if (safe) printf(">>%s<<\n", safe); if (safe) n = int(safe-pBuffer); } // copy bytes to file //printf("Copying %d bytes\n", n); if (n>0) fwrite(pBuffer, 1, n, pFile); if (n<pBufferPos) { int nHeader = n; int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; pBuffer[pBufferPos] = 0; } else { pBufferPos = 0; } if (bd) { // close file fclose(pFile); sendHTML("<html><body><h1>Thank you!</h1></body></html>"); pState = WEB_IDLE; pBufferPos = 0; } } return 1; // start is "POST /upload " // wait for line "Content-Type:", then find "boundary=" in line, key is the remainder of the line // wait for line "Content-Length:", then get the number in bytes // wait for line "--" + key // wait for line "Content-Disposition:", find "filename=", get filename // wait for empty line // read 'length' bytes // wait for line "--" + key + "--" // reply, so sender know we are fine /* POST /upload HTTP/1.1 Host: localhost:8080 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7BlGom6EjFm9Oydz Origin: http://localhost:8080 Accept-Encoding: gzip, deflate Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,x/x;q=0.8 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4 Referer: http://localhost:8080/upload Content-Length: 231 Accept-Language: en-us ------WebKitFormBoundary7BlGom6EjFm9Oydz Content-Disposition: form-data; name="fileID"; filename="00test.txt" Content-Type: text/plain The quick brown fox jumps over the lazy dog. ------WebKitFormBoundary7BlGom6EjFm9Oydz-- */ }
void RssObject::getUrl(QString url) { emit sendHTML(url); }