int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); //create socket if (sockfd < 0) error("ERROR opening socket"); memset((char *) &serv_addr, 0, sizeof(serv_addr)); //reset memory //fill in address info portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); //5 simultaneous connection at most while(1) { //accept connections newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); int n; char buffer[256]; memset(buffer, 0, 256); //reset memory //read client's message n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); //reply to client n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); generateResponse(newsockfd, "hello.html", 50); close(newsockfd);//close connection } close(sockfd); return 0; }
string HttpResponse::generateResponse(string httpMethod, HttpRequest *req, bool appendHeaders /*= true*/) { if(httpMethod=="HEAD" && appendHeaders) { return generateHeadResponse(); } else if(httpMethod=="OPTIONS" && appendHeaders) { return generateOptionsResponse(); } else if(httpMethod=="TRACE" && appendHeaders) { return generateTraceResponse(req); } else { return generateResponse(appendHeaders); } }
void handleRequest(session_t* session, int connectFd, char* resource, char* postData) { gchar** data = g_strsplit(resource, "?", 2); gchar* file = data[0]; GHashTable* query = parseQueryString(data[1]); bool hasBG = false; bool isSlashTest = false; if (data[1] == NULL && (g_strcmp0(data[0], "/") == 0)) { generateResponse(session, connectFd, resource, NULL, false, "200", isSlashTest, query, postData); } else { if (g_strcmp0(file, "/color") == 0) { gchar* color = g_hash_table_lookup(query, "bg"); if (color != NULL) { // URI contains /color?bg=x hasBG = true; generateResponse(session, connectFd, resource, color, hasBG, "200", isSlashTest, query, postData); } else { // URI does not contains bg=(...) // Check if request contains cookie gchar* cookie = g_hash_table_lookup(session->headers, "Cookie"); if (cookie == NULL) { generateResponse(session, connectFd, resource, NULL, hasBG, "200", isSlashTest, query, postData); } else { gchar **pairs = g_strsplit(cookie, "; ", 100); gchar** pair; int size = g_strv_length(pairs), i; for (i = 0; i < size; i++) { pair = g_strsplit(pairs[i], "=", 2); if (g_strcmp0(pair[0], "color") == 0) { generateResponse(session, connectFd, resource, pair[1], 1, "200", isSlashTest, query, postData); break; } } } } } else if (g_strcmp0(file, "/test") == 0) { isSlashTest = true; generateResponse(session, connectFd, resource, NULL, hasBG, "200", isSlashTest, query, postData); } else { generateResponse(session, connectFd, resource, NULL, false, "404", isSlashTest, query, postData); } } }
void SessionManager::handleRead() { int count = tcpSocket->bytesAvailable(); if(count <= 0) return; char dataIn[count+1]; memset(dataIn, 0, count+1); //从socket中读取数据 tcpSocket->read(dataIn, count); QString data = QString(dataIn); /*打印接收到的服务器数据*/ std::cerr << "<!-- IN -->\n" << dataIn << "\n\n"; //对数据进行预处理 /*如果数据包交换已经结束,即已经收到</stream:stream>数据包*/ if(data.contains(QString("</stream:stream>"), Qt::CaseInsensitive)) { /***************************************************** *我们仍然需要处理</stream:stream>之前的部分数据 *此处我们暂时不考虑,但是要注意时候必须补上,此处一定会出错! **************************************************/ tcpSocket->close(); return; } /*如果是第一个数据包,必须在后面加一个</stream:stream>, 否则解析的过程中会报错*/ if(data.contains(QString("<?xml version='1.0'?>"), Qt::CaseInsensitive)) { data += QString("</stream:stream>"); } /** *为了能正确的解析数据,对数据进行预处理 *所有的非初始数据都将被<stream:stream />对包裹 */ else data = QString("<stream:stream>")+data+QString("</stream:stream>"); /*解析xml流,生成xml树*/ xmlTree->clear(); QXmlInputSource inputSource; inputSource.setData(data); QXmlSimpleReader reader; reader.setContentHandler(xmlHandler); reader.setErrorHandler(xmlHandler); reader.parse(inputSource); /*解析xmlTree,生成状态信息*/ if(!parseTree()) { QMessageBox::warning(0, tr("Error"), tr("Error: %1\n").arg(getError())); return; } /*根据解析生成的状态信息,生成回复*/ outData = generateResponse(); if(status >= PRESENCEUPDATED) return; if(outData.isEmpty()) { QMessageBox::warning(0, tr("Error"), tr("Error: %1\n").arg(getError())); }else{ emit send(outData); } }