void showPage(char* filename, unsigned filenameLen) {
	printf("Request file: %s (%u)\n", filename, filenameLen);

	// CONTENT
	char* html = 0;
	unsigned html_len;
	char* contentType;
	if (memcmp(filename,"/",filenameLen)==0) { // index page
		char data[] = "<html><head><title>Testseite</title></head><body><h1>Testseite</h1><p>Hier steht Text</p><a href='speedtest.bin'>Lade große Datenmenge für Speedtest</a></body></html>";
		html_len = sizeof(data)-1;
		html = data;
		contentType = "text/html; charset=utf-8;";
	} else if (memcmp(filename,"/speedtest.bin",filenameLen)==0) { // speedtest file
		html_len = 1024*1024*10; // 10 MB
		contentType = "application/octet-stream";
	} else { // not found
		html_len = 0;
	}

	// HEADER
	unsigned remainingLength = 1024;
	char output[remainingLength];
	char* pout = output;
	bool ok = true;
	
	if (!html_len)
		ok &= addErrorResponseHeader(pout, remainingLength);
	else
		ok &= addResponseHeader(pout, remainingLength, html_len);
	ok &= addContentLanguageHeader(pout, remainingLength);
	ok &= addConnectionClose(pout, remainingLength);
	if (html_len)
		ok &= addContentType(pout, remainingLength, contentType);
	ok &= closeHeader(pout, remainingLength);
	
	if (!ok) {
		while(socket.close() == false);
		return;
	}

	if (html) {
		// html
		memcpy(pout, html, html_len);
	}
	
	printf("Send %u bytes. Is 404? %u\n", pout-output+html_len, html_len==0);
	socket.write(output, pout-output+html_len);
	while(socket.close() == false);
}
예제 #2
0
bool HttpResponse::addHeading ()
{
	bool more = false;

	if ( getAttributes() & FILE_ATTRIBUTE_HIDDEN )
	{
		// never show hidden files
		addError( idHttpForbidden );
	}
	else if ( getAttributes() & FILE_ATTRIBUTE_DIRECTORY )
	{
		if ( _server->getAllowListing() )
		{
			// create a directory listing
			addStatus( idHttpOk );
			addString( CRLF );
			more = true;
		}
		else
			addError( idHttpForbidden );
	}
	else
	{
		LPCTSTR fileName = getFullPath().c_str();

		if ( _file.openFastRead(getFullPath()) )
		{
			if ( getStatus() != REQ_SIMPLE )
			{
				TimeStamp timeIfMod;

				string strIfMod = getHeaderValue( idParamIfModifiedSince );

				if ( strIfMod.size() > 0 &&
					HttpUtil::fromHttpTime( strIfMod, timeIfMod ) &&
					!HttpUtil::ifModSince( _file, timeIfMod ) )
				{
					// eh, it hasn't been modified
					addStatus( idHttpNotModified );

					// don't need it anymore
					_file.close();
				}
				else
				{
					// send it off
					addStatus( idHttpOk );

					// set content type and length
					long contentLength = _file.size();

					addContentType();
					addHeader( idParamContentLength, contentLength );

					// get the last modified time
					FILETIME ft;
					if ( GetFileTime( _file, NULL, NULL, &ft ) )
					{
						string strTime = HttpUtil::getHttpDate( &ft );
						addHeader( idParamLastModified, strTime );
					}

					more = true;
				}

				// blank line
				addString( CRLF );
			}
			else
			{
				more = true;
			}
		}
		else
		{
			// couldn't open; try again later
			addError( idHttpUnavailable );
		}
	}
	return more;
}