Esempio n. 1
0
static void readFlashPage(UINT32 *chunksInPage, UINT32 *srcChunkByteOffsets, UINT32 *chunkIdxs, UINT8 *decreaseInOW)
{
    uart_print("readFlashPage\r\n");
    if(*chunksInPage > 2)
    { // it's convenient to read the entire page
        readCompletePage(chunksInPage, srcChunkByteOffsets, chunkIdxs, decreaseInOW);
    }
    else
    { // read just one chunk
        readOneChunk(chunksInPage, srcChunkByteOffsets, chunkIdxs, decreaseInOW);
    }
}
// Do HTTP method.
void UploadFileServlet::doMethod(IHttpServletRequest& request, IHttpServletResponse& response){
	if (!checkRequest(request, response)){
		return;
	}
	// Create the file. The current file will be overrided.
	std::string name = request.getParameter("file_name");
	createFolderForFile(folder + name);
	std::ofstream fout;
	fout.open(folder + name, std::ofstream::out | std::ofstream::binary);
	if (!fout){
		response.sendErrorWithMessage(500, "Cannot open the file.");
		return;
	}
	// Read input
	bool error = true;
	std::istream& in = request.getInputStream();
	std::string chunkSizeLine;
	std::string tmp;
	int chunkSize;
	std::getline(in, chunkSizeLine);
	char* buf = NULL;
	int bufSize = 0;
	while (in){
		if (!readOneChunk(in, chunkSize, chunkSizeLine, error, buf, bufSize, fout, response)){
			break;
		}
		// Read the next chunk
		std::getline(in, chunkSizeLine);
	}
	if (buf != NULL){
		delete buf;
	}
	// close the file
	fout.close();
	// Send error if occurred
	if (error){
		if (!response.isCommitted()){
			response.sendErrorWithMessage(400, "Wrong chunked format");
		}
		// throw an exception to close connection
		throw std::logic_error("Wrong chunked format");
	}
	else{
		LOG(INFO) << "Saved the file \"" << folder << name << "\".";
	}
}