Ejemplo n.º 1
0
	void HttpClientHandler::sendResponse(const HttpResponseHeader & hdr)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending response " << hdr.toString() << endl;
		QTextStream os(client);
		os.setEncoding( QTextStream::UnicodeUTF8 );
		os << hdr.toString();
	}
Ejemplo n.º 2
0
	void HttpClientHandler::executePHPScript(
			PhpInterface* php_iface,
			HttpResponseHeader & hdr,
			const QString & php_exe,
			const QString & php_file,
			const QMap<QString,QString> & args)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Launching PHP script " << php_file << endl;
		php = new PhpHandler(php_exe,php_iface);
		if (!php->executeScript(php_file,args))
		{
			QString data = QString(HTTP_500_ERROR).arg("Failed to launch PHP executable !");
			hdr.setResponseCode(500);
			hdr.setValue("Content-Length",QString::number(data.utf8().length()));
			
			QTextStream os(client);
			os.setEncoding( QTextStream::UnicodeUTF8 );
			os << hdr.toString();
			os << data;
			state = WAITING_FOR_REQUEST;
		}
		else
		{
			php_response_hdr = hdr;
			connect(php,SIGNAL(finished()),this,SLOT(onPHPFinished()));
			state = PROCESSING_PHP;
		}
	}
Ejemplo n.º 3
0
	void HttpClientHandler::sendResponse(HttpResponseHeader & hdr)
	{
		setResponseHeaders(hdr);
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending response " << hdr.toString() << endl;
		output_buffer.append(hdr.toString().toUtf8());
		sendOutputBuffer();
	}
Ejemplo n.º 4
0
	bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl;
		setResponseHeaders(hdr);
		// first look in cache
		MMapFile* c = srv->cacheLookup(full_path);
		
		if (!c)
		{
			// not in cache so load it
			c = new MMapFile();
			if (!c->open(full_path,QIODevice::ReadOnly))
			{
				delete c;
				Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl;
				return false;
			}
			srv->insertIntoCache(full_path,c);
		}
		
	//	Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl;
	//	Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl;
				
		QByteArray data((const char*)c->getDataPointer(),c->getSize());	
		hdr.setValue("Content-Length",QString::number(data.size()));
		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data);
		
		sendOutputBuffer();
	//	Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl;
		return true;
	}
Ejemplo n.º 5
0
	void HttpClientHandler::send(HttpResponseHeader & hdr,const QByteArray & data)
	{
		setResponseHeaders(hdr);
		hdr.setValue("Content-Length",QString::number(data.length()));
		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data);
		sendOutputBuffer();
	}
Ejemplo n.º 6
0
	void HttpClientHandler::send500(HttpResponseHeader & hdr)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 500 " << endl;
		QString data = QString(HTTP_500_ERROR).arg("An internal server error occured !");
		hdr.setValue("Content-Length",QString::number(data.length()));

		QTextStream os(client);
		os.setEncoding( QTextStream::UnicodeUTF8 );
		os << hdr.toString();
		os << data;
	}
Ejemplo n.º 7
0
	void HttpClientHandler::send404(HttpResponseHeader & hdr,const QString & path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 404 " << path << endl;
		QString data = HTTP_404_ERROR;
		hdr.setValue("Content-Length",QString::number(data.length()));

		QTextStream os(client);
		os.setEncoding( QTextStream::UnicodeUTF8 );
		os << hdr.toString();
		os << data;
	}
Ejemplo n.º 8
0
	void HttpClientHandler::send404(HttpResponseHeader & hdr,const QString & path)
	{
		setResponseHeaders(hdr);
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 404 " << path << endl;
		QString data = QString(HTTP_404_ERROR).arg(path);
		hdr.setValue("Content-Length",QString::number(data.length()));

		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data.toUtf8());
		sendOutputBuffer();
	}
Ejemplo n.º 9
0
	void HttpClientHandler::send500(HttpResponseHeader & hdr,const QString & error)
	{
		setResponseHeaders(hdr);
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 500 " << endl;
		QString err = i18n("An internal server error occurred: %1",error);
		QString data = QString(HTTP_500_ERROR).arg(err);
		hdr.setValue("Content-Length",QString::number(data.length()));

		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data.toUtf8());
		sendOutputBuffer();
	}
Ejemplo n.º 10
0
	bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl;
		// first look in cache
		MMapFile* c = srv->cacheLookup(full_path);
		
		if (!c)
		{
			// not in cache so load it
			c = new MMapFile();
			if (!c->open(full_path,MMapFile::READ))
			{
				delete c;
				Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl;
				return false;
			}
			srv->insertIntoCache(full_path,c);
		}
		
		hdr.setValue("Content-Length",QString::number(c->getSize()));
		
	//	Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl;
	//	Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl;
				
		QCString d = hdr.toString().utf8();
		client->writeBlock(d.data(),d.length());

		Uint32 written = 0;
		Uint32 total = c->getSize();
		const char* data = (const char*)c->getDataPointer();
		while (written < total)
		{
			Uint32 w = client->writeBlock(data + written,total - written);
			written += w;
		}
		client->flush();
	//	Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl;
		return true;
	}
Ejemplo n.º 11
0
static void runHttpHeaderTest(TestRunner& tr)
{
   tr.group("HttpHeader");

   tr.test("Bicapitalization");
   {
      // test bicapitalization of http headers
      const char* tests[] = {
         "", "",
         "a", "A",
         "-", "-",
         "a--a", "A--A",
         "-aa-", "-Aa-",
         "-aa", "-Aa",
         "aa-", "Aa-",
         "aaa-zzz", "Aaa-Zzz",
         "ThIs-a-BICaPitAlized-hEADer", "This-A-Bicapitalized-Header",
         "Message-ID", "Message-Id",
         NULL
      };
      for(int i = 0; tests[i] != NULL; i +=2)
      {
         char* bic = strdup(tests[i]);
         HttpHeader::biCapitalize(bic);
         assertStrCmp(bic, tests[i+1]);
         free(bic);
      }
   }
   tr.passIfNoException();

   tr.test("HttpRequestHeader parse");
   {
      HttpRequestHeader header;
      header.setDate();
      header.setMethod("GET");
      header.setPath("/");
      header.setVersion("HTTP/1.1");
      header.setField("host", "localhost:80");
      header.setField("Content-Type", "text/html");
      header.setField("Connection", "close");

      string date;
      string expect;
      expect.append("GET / HTTP/1.1\r\n");
      expect.append("Connection: close\r\n");
      expect.append("Content-Type: text/html\r\n");
      expect.append("Date: ");
      header.getField("Date", date);
      expect.append(date);
      expect.append("\r\n");
      expect.append("Host: localhost:80\r\n");
      expect.append("\r\n");

      string str = header.toString();
      assertStrCmp(str.c_str(), expect.c_str());

      HttpRequestHeader header2;
      header2.parse(str);

      string str2 = header2.toString();
      assertStrCmp(str2.c_str(), expect.c_str());
   }
   tr.passIfNoException();

   tr.test("HttpResponseHeader parse");
   {
      HttpResponseHeader header;
      header.setDate();
      header.setVersion("HTTP/1.1");
      header.setStatus(404, "Not Found");
      header.setField("host", "localhost:80");
      header.setField("Content-Type", "text/html");
      header.setField("Connection", "close");

      string date;
      string expect;
      expect.append("HTTP/1.1 404 Not Found\r\n");
      expect.append("Connection: close\r\n");
      expect.append("Content-Type: text/html\r\n");
      expect.append("Date: ");
      header.getField("Date", date);
      expect.append(date);
      expect.append("\r\n");
      expect.append("Host: localhost:80\r\n");
      expect.append("\r\n");

      string str = header.toString();
      assertStrCmp(str.c_str(), expect.c_str());

      HttpResponseHeader header2;
      header2.parse(str);

      string str2 = header2.toString();
      assertStrCmp(str2.c_str(), expect.c_str());
   }
   tr.passIfNoException();

   tr.test("Multiple fields with same name");
   {
      HttpResponseHeader header;
      header.setDate();
      header.setVersion("HTTP/1.1");
      header.setStatus(404, "Not Found");
      header.setField("host", "localhost:80");
      header.setField("Content-Type", "text/html");
      header.setField("Connection", "close");
      header.addField("Set-Cookie", "cookie1=value1; max-age=0; path=/");
      header.addField("Set-Cookie", "cookie2=value2; max-age=0; path=/");
      header.addField("Set-Cookie", "cookie3=value3; max-age=0; path=/");

      string date;
      string expect;
      expect.append("HTTP/1.1 404 Not Found\r\n");
      expect.append("Connection: close\r\n");
      expect.append("Content-Type: text/html\r\n");
      expect.append("Date: ");
      header.getField("Date", date);
      expect.append(date);
      expect.append("\r\n");
      expect.append("Host: localhost:80\r\n");
      expect.append("Set-Cookie: cookie1=value1; max-age=0; path=/\r\n");
      expect.append("Set-Cookie: cookie2=value2; max-age=0; path=/\r\n");
      expect.append("Set-Cookie: cookie3=value3; max-age=0; path=/\r\n");
      expect.append("\r\n");

      string str = header.toString();
      assertStrCmp(str.c_str(), expect.c_str());

      HttpResponseHeader header2;
      header2.parse(str);

      string str2 = header2.toString();
      assertStrCmp(str2.c_str(), expect.c_str());
   }
   tr.passIfNoException();

   tr.test("hasContentType");
   {
      HttpResponseHeader header;

      header.clearFields();
      header.setField("Content-Type", "text/html");
      assert(header.hasContentType("text/html"));

      header.clearFields();
      header.setField("Content-Type", "text/html; params");
      assert(header.hasContentType("text/html"));

      header.clearFields();
      header.setField("Content-Type", "text/html; params");
      assert(!header.hasContentType("text/plain"));
   }
   tr.passIfNoException();

   tr.ungroup();
}