Ejemplo n.º 1
0
// 404
void respErrorPage( headerOut *header_out , int status_code )
{
	if (status_code >= 400 && status_code <= 507)
	{
		int ret = respDefinedErrorPage( header_out , status_code );
		if (ret == AE_TRUE)
		{
			return;
		}
	}
	
	// get header info
	headerStatus error_page = getHttpStatus( status_code );
	int datalen = strlen( error_page.data );
	
	// header append
	createCommonHeader( header_out , status_code );
	headerAppendLength( header_out , datalen );
	appendRespHeader( header_out , HEADER_END_LINE );
	
	// send
	httpResponseWrite( header_out->req->connfd , header_out->data ,
			header_out->length );
	if (header_out->req->nobody != AE_TRUE)
	{
		httpResponseWrite( header_out->req->connfd , error_page.data , datalen );
	}
	// nobody强制关闭
	httpClose( header_out->req , header_out->req->nobody );
}
Ejemplo n.º 2
0
void httpRedirect( httpHeader *req_header , char *uri )
{
	headerOut header_out;
	memset( &header_out , 0 , sizeof( header_out ) );
	header_out.req = req_header;
	headerStatus error_page = getHttpStatus( 301 ); // header append
			
	createCommonHeader( &header_out , 301 );
	appendRespHeader( &header_out , HEADER_LOCATION , uri );
	appendRespHeader( &header_out , HEADER_CONTENT_LENGTH , 0 );
	appendRespHeader( &header_out , HEADER_END_LINE );
	
	httpResponseWrite( req_header->connfd , header_out.data , header_out.length );
	httpClose( req_header , 0 );
}
Ejemplo n.º 3
0
int main(int argc, const char * argv[])
{
	printVersion();
	// check number of arguments and if all arguments can be read
	if (argc < 3 || !readArguments(argc, argv))
	{
		printUsage();
		return -1;
	}
	// check if the input path exist
	if (!FS_NAMESPACE::exists(inFilePath))
	{
		std::cout << "Error: Invalid input file/directory \"" << inFilePath.string() << "\"!" << std::endl;
		return -2;
	}
	if (createBinary)
	{
		// check if argument 2 is a file
		if (FS_NAMESPACE::is_directory(outFilePath))
		{
			std::cout << "Error: Output must be a file if -b is used!" << std::endl;
			return -2;
		}
	}
	else if (appendFile)
	{
		// check if argument 2 is a file
		if (FS_NAMESPACE::is_directory(outFilePath))
		{
			std::cout << "Error: Output must be a file if -a is used!" << std::endl;
			return -2;
		}
	}
	else if (FS_NAMESPACE::is_directory(inFilePath) != FS_NAMESPACE::is_directory(outFilePath))
	{
		// check if output directory exists
		if (FS_NAMESPACE::is_directory(outFilePath) && !FS_NAMESPACE::exists(outFilePath))
		{
			std::cout << "Error: Invalid output directory \"" << outFilePath.string() << "\"!" << std::endl;
			return -2;
		}
		// check if arguments 1 and 2 are both files or both directories
		std::cout << "Error: Input and output file must be both either a file or a directory!" << std::endl;
		return -2;
	}
	if (appendFile)
	{
		// append file a to b
		if (!appendAtoB(outFilePath, inFilePath))
		{
			std::cout << "Error: Failed to append data to executable!" << std::endl;
			return -3;
		}
	}
	else
	{
		// build list of files to process
		std::vector<FileData> fileList;
		if (FS_NAMESPACE::is_directory(inFilePath) && FS_NAMESPACE::is_directory(inFilePath))
		{
			// both files are directories, build file ist
			fileList = getFileDataFrom(inFilePath, outFilePath, inFilePath, useRecursion);
			if (fileList.empty())
			{
				std::cout << "Error: No files to convert!" << std::endl;
				return -3;
			}
		}
		else
		{
			// just add single input/output file
			FileData temp;
			temp.inPath = inFilePath;
			temp.outPath = outFilePath;
			temp.internalName = inFilePath.filename().string(); // remove all, but the file name and extension
			if (beVerbose)
			{
				std::cout << "Found input file " << inFilePath << std::endl;
				std::cout << "Internal name will be \"" << temp.internalName << "\"" << std::endl;
				std::cout << "Output path is " << temp.outPath << std::endl;
			}
			// get file size
			try
			{
				temp.size = static_cast<uint64_t>(FS_NAMESPACE::file_size(inFilePath));
				if (beVerbose)
				{
					std::cout << "Size is " << temp.size << " bytes." << std::endl;
				}
			}
			catch (...)
			{
				std::cout << "Error: Failed to get size of " << inFilePath << "!" << std::endl;
				temp.size = 0;
			}
			fileList.push_back(temp);
		}

		// does the user want an binary file?
		if (createBinary)
		{
			// yes. build it.
			if (!createBlob(fileList, outFilePath))
			{
				std::cout << "Error: Failed to convert to binary file!" << std::endl;
				return -4;
			}
		}
		else
		{
			// no. convert files to .c/.cpp. loop through list, converting files
			for (auto fdIt = fileList.begin(); fdIt != fileList.cend(); ++fdIt)
			{
				if (!convertFile(*fdIt, commonHeaderFilePath))
				{
					std::cout << "Error: Failed to convert all files. Aborting!" << std::endl;
					return -4;
				}
			}
			// do we need to write a header file?
			if (!commonHeaderFilePath.empty())
			{
				if (!createCommonHeader(fileList, commonHeaderFilePath, !utilitiesFilePath.empty(), useC))
				{
					return -5;
				}
				// do we need to create utilities?
				if (!utilitiesFilePath.empty())
				{
					if (!createUtilities(fileList, utilitiesFilePath, commonHeaderFilePath, useC, combineResults))
					{
						return -6;
					}
				}
			}
		}
	} // if (!appendFile) {
	// profit!!!
	std::cout << "res2h succeeded." << std::endl;
	return 0;
}
Ejemplo n.º 4
0
void httpResponseStaticProc( httpHeader *req_header )
{
	
	int len,cllen,ctlen;
	char path[1024] =
			{0};
	headerOut header_out;
	memset( &header_out , 0 , sizeof( header_out ) );
	header_out.req = req_header;
	
	getFilePath( req_header->uri , path );
	struct stat stat_file;
	int ret = stat( path , &stat_file );
	
	if (ret < 0)
	{
		respErrorPage( &header_out , 404 );
		return;
	}
	
	createCommonHeader( &header_out , 200 );
	headerAppendLength( &header_out , stat_file.st_size );
	appendRespHeader( &header_out , HEADER_END_LINE );
	
	int nwritten = write( req_header->connfd , header_out.data , header_out.length );
	if (nwritten <= 0)
	{
		printf( "I/O error writing to client connfd=%d,len=%d: %s \n" ,
				req_header->connfd , header_out.length , strerror( errno ) );
		return;
	}
	
	if (req_header->nobody == AE_TRUE)
	{
		httpClose( req_header , 1 );
		return;
	}
	
	int fd = open( path , O_RDONLY );
	if (fd < 0)
	{
		printf( "Open file Error:%s,errno=%d \n" , strerror( errno ) , errno );
		return;
	}
	
	// setsockopt (fd, SOL_TCP, TCP_CORK, &on, sizeof (on));
	off_t offset = 0;
	int force_close = 0;
	while (offset < stat_file.st_size)
	{
		int sendn =
				sendfile( req_header->connfd , fd , &offset , stat_file.st_size - offset );
		
		if (sendn < 0)
		{
			//如果socket缓冲区不可用,则挂起等待可用
			if (errno == EAGAIN || errno == EINTR)
			{
				if (anetHandup( req_header->connfd , 5000 , AE_WRITABLE ) < 0)
				{
					//如果超时,退出
					printf( "Sendfile anetHandup timeout.......\n" );
					force_close = 1;
					break;
				}
				else
				{
					//否则继续发送
					continue;
				}
			}
			else
			{
				break;
			}
		}
	}
	close( fd );
	httpClose( req_header , force_close );
}