本文整理汇总了C++中NPT_InputStreamReference类的典型用法代码示例。如果您正苦于以下问题:C++ NPT_InputStreamReference类的具体用法?C++ NPT_InputStreamReference怎么用?C++ NPT_InputStreamReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NPT_InputStreamReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: NPT_COMPILER_UNUSED
/*----------------------------------------------------------------------
| CMediaCrawler::ProcessFileRequest
+---------------------------------------------------------------------*/
NPT_Result
CMediaCrawler::ProcessFileRequest(NPT_HttpRequest& request,
NPT_HttpResponse& response,
NPT_SocketInfo& info)
{
NPT_COMPILER_UNUSED(info);
NPT_LOG_FINE("CMediaCrawler::ProcessFileRequest Received Request:");
PLT_LOG_HTTP_MESSAGE(NPT_LOG_LEVEL_FINE, &request);
if (request.GetMethod().Compare("GET") && request.GetMethod().Compare("HEAD")) {
response.SetStatus(500, "Internal Server Error");
return NPT_SUCCESS;
}
// add the user agent header, some stupid media servers like YME needs it
if (!request.GetHeaders().GetHeader(NPT_HTTP_HEADER_USER_AGENT)) {
request.GetHeaders().SetHeader(NPT_HTTP_HEADER_USER_AGENT,
"Platinum/" PLT_PLATINUM_VERSION_STRING);
}
// File requested
NPT_HttpResponse* out_response = NULL;
NPT_HttpUrlQuery query(request.GetUrl().GetQuery());
const char* url = query.GetField("url");
if (url) {
// look for handler
CStreamHandler* handler = NULL;
NPT_ContainerFind(m_StreamHandlers, CStreamHandlerFinder(NULL, url), handler);
if (handler && NPT_SUCCEEDED(handler->ProcessFileRequest(request, out_response)) && out_response) {
// copy response code and reason
response.SetStatus(out_response->GetStatusCode(), out_response->GetReasonPhrase());
// copy headers
NPT_List<NPT_HttpHeader*>::Iterator headers = out_response->GetHeaders().GetHeaders().GetFirstItem();
while (headers) {
response.GetHeaders().SetHeader((*headers)->GetName(), (*headers)->GetValue());
++headers;
}
response.SetEntity(new NPT_HttpEntity(response.GetHeaders()));
// update inputstream
NPT_HttpEntity* out_entity;
if ((out_entity = out_response->GetEntity()) != NULL) {
NPT_InputStreamReference inputstream;
out_entity->GetInputStream(inputstream);
if (!inputstream.IsNull()) {
// set the stream but do not update the content length
response.GetEntity()->SetInputStream(inputstream, false);
}
}
delete out_response;
return NPT_SUCCESS;
}
}
response.SetStatus(404, "File Not Found");
return NPT_SUCCESS;
}
示例2: NPT_LOG_WARNING_1
/*----------------------------------------------------------------------
| NPT_ZipFile::GetInputStream
+---------------------------------------------------------------------*/
NPT_Result
NPT_ZipFile::GetInputStream(Entry& entry, NPT_InputStreamReference& zip_stream, NPT_InputStream*& file_stream)
{
// default return value
file_stream = NULL;
// we don't support encrypted files
if (entry.m_Flags & NPT_ZIP_FILE_FLAG_ENCRYPTED) {
return NPT_ERROR_NOT_SUPPORTED;
}
// check that we support the compression method
#if NPT_CONFIG_ENABLE_ZIP
if (entry.m_CompressionMethod != NPT_ZIP_FILE_COMPRESSION_METHOD_NONE &&
entry.m_CompressionMethod != NPT_ZIP_FILE_COMPRESSION_METHOD_DEFLATE) {
return NPT_ERROR_NOT_SUPPORTED;
}
#else
if (entry.m_CompressionMethod != NPT_ZIP_FILE_COMPRESSION_METHOD_NONE) {
return NPT_ERROR_NOT_SUPPORTED;
}
#endif
// seek to the start of the file entry
NPT_Result result = zip_stream->Seek(entry.m_RelativeOffset);
if (NPT_FAILED(result)) {
NPT_LOG_WARNING_1("seek failed (%d)", result);
return result;
}
// read the fixed part of the header
unsigned char header[30];
result = zip_stream->ReadFully(header, 30);
if (NPT_FAILED(result)) {
NPT_LOG_WARNING_1("read failed (%d)", result);
return result;
}
NPT_UInt16 file_name_length = NPT_BytesToInt16Le(&header[26]);
NPT_UInt16 extra_field_length = NPT_BytesToInt16Le(&header[28]);
unsigned int header_size = 30+file_name_length+extra_field_length;
NPT_LargeSize zip_stream_size = 0;
zip_stream->GetSize(zip_stream_size);
if (entry.m_RelativeOffset+header_size+entry.m_CompressedSize > zip_stream_size) {
// something's wrong here
return NPT_ERROR_INVALID_FORMAT;
}
file_stream = new NPT_SubInputStream(zip_stream, entry.m_RelativeOffset+header_size, entry.m_CompressedSize);
#if NPT_CONFIG_ENABLE_ZIP
if (entry.m_CompressionMethod == NPT_ZIP_FILE_COMPRESSION_METHOD_DEFLATE) {
NPT_InputStreamReference file_stream_ref(file_stream);
file_stream = new NPT_ZipInflatingInputStream(file_stream_ref, true);
}
#endif
return NPT_SUCCESS;
}
示例3: tmp_context
/*----------------------------------------------------------------------
| PLT_HttpServer::ServeFile
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpServer::ServeFile(const NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse& response,
NPT_String file_path)
{
NPT_InputStreamReference stream;
NPT_File file(file_path);
NPT_FileInfo file_info;
// prevent hackers from accessing files outside of our root
if ((file_path.Find("/..") >= 0) || (file_path.Find("\\..") >= 0) ||
NPT_FAILED(NPT_File::GetInfo(file_path, &file_info))) {
return NPT_ERROR_NO_SUCH_ITEM;
}
// check for range requests
const NPT_String* range_spec = request.GetHeaders().GetHeaderValue(NPT_HTTP_HEADER_RANGE);
// handle potential 304 only if range header not set
NPT_DateTime date;
NPT_TimeStamp timestamp;
if (NPT_SUCCEEDED(PLT_UPnPMessageHelper::GetIfModifiedSince((NPT_HttpMessage&)request, date)) &&
!range_spec) {
date.ToTimeStamp(timestamp);
NPT_LOG_INFO_5("File %s timestamps: request=%d (%s) vs file=%d (%s)",
(const char*)request.GetUrl().GetPath(),
(NPT_UInt32)timestamp.ToSeconds(),
(const char*)date.ToString(),
(NPT_UInt32)file_info.m_ModificationTime,
(const char*)NPT_DateTime(file_info.m_ModificationTime).ToString());
if (timestamp >= file_info.m_ModificationTime) {
// it's a match
NPT_LOG_FINE_1("Returning 304 for %s", request.GetUrl().GetPath().GetChars());
response.SetStatus(304, "Not Modified", NPT_HTTP_PROTOCOL_1_1);
return NPT_SUCCESS;
}
}
// open file
if (NPT_FAILED(file.Open(NPT_FILE_OPEN_MODE_READ)) ||
NPT_FAILED(file.GetInputStream(stream)) ||
stream.IsNull()) {
return NPT_ERROR_NO_SUCH_ITEM;
}
// set Last-Modified and Cache-Control headers
if (file_info.m_ModificationTime) {
NPT_DateTime last_modified = NPT_DateTime(file_info.m_ModificationTime);
response.GetHeaders().SetHeader("Last-Modified", last_modified.ToString(NPT_DateTime::FORMAT_RFC_1123), true);
response.GetHeaders().SetHeader("Cache-Control", "max-age=0,must-revalidate", true);
//response.GetHeaders().SetHeader("Cache-Control", "max-age=1800", true);
}
PLT_HttpRequestContext tmp_context(request, context);
return ServeStream(request, context, response, stream, PLT_MimeType::GetMimeType(file_path, &tmp_context));
}
示例4:
/*----------------------------------------------------------------------
| NPT_File::Load
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::Load(NPT_DataBuffer& buffer)
{
NPT_InputStreamReference input;
// get the input stream for the file
NPT_CHECK_FATAL(GetInputStream(input));
// read the stream
return input->Load(buffer);
}
示例5: file
/*----------------------------------------------------------------------
| PLT_FileMediaServer::OnAlbumArtRequest
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServer::OnAlbumArtRequest(NPT_HttpResponse& response,
NPT_String file_path)
{
NPT_LargeSize total_len;
NPT_File file(file_path);
NPT_InputStreamReference stream;
// prevent hackers from accessing files outside of our root
if ((file_path.Find("/..") >= 0) || (file_path.Find("\\..") >= 0)) {
return NPT_FAILURE;
}
if (NPT_FAILED(file.Open(NPT_FILE_OPEN_MODE_READ)) ||
NPT_FAILED(file.GetInputStream(stream)) ||
NPT_FAILED(stream->GetSize(total_len)) || (total_len == 0)) {
goto filenotfound;
} else {
NPT_String extension = NPT_FilePath::FileExtension(file_path);
if (extension.GetLength() == 0) {
goto filenotfound;
}
PLT_MetadataHandler* metadataHandler = NULL;
char* caData;
int caDataLen;
NPT_Result ret = NPT_ContainerFind(m_MetadataHandlers,
PLT_MetadataHandlerFinder(extension),
metadataHandler);
if (NPT_FAILED(ret) || metadataHandler == NULL) {
goto filenotfound;
}
// load the metadatahandler and read the cover art
if (NPT_FAILED(metadataHandler->Load(*stream)) ||
NPT_FAILED(metadataHandler->GetCoverArtData(caData, caDataLen))) {
goto filenotfound;
}
PLT_HttpHelper::SetContentType(response, "application/octet-stream");
PLT_HttpHelper::SetBody(response, caData, caDataLen);
delete caData;
return NPT_SUCCESS;
}
filenotfound:
response.SetStatus(404, "File Not Found");
return NPT_SUCCESS;
}
示例6:
/*----------------------------------------------------------------------
| PLT_HttpHelper::IsBodyStreamSeekable
+---------------------------------------------------------------------*/
bool
PLT_HttpHelper::IsBodyStreamSeekable(NPT_HttpMessage* message)
{
NPT_HttpEntity* entity = message->GetEntity();
NPT_InputStreamReference stream;
if (!entity || NPT_FAILED(entity->GetInputStream(stream))) return true;
// try to get current position and seek there
NPT_Position position;
if (NPT_FAILED(stream->Tell(position)) ||
NPT_FAILED(stream->Seek(position))) {
return false;
}
return true;
}
示例7: NPT_COMPILER_UNUSED
/*----------------------------------------------------------------------
| PLT_Downloader::ProcessResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_Downloader::ProcessResponse(NPT_Result res,
const NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse* response)
{
NPT_COMPILER_UNUSED(request);
NPT_COMPILER_UNUSED(context);
if (NPT_FAILED(res)) {
NPT_LOG_WARNING_2("Downloader error %d for %s", res, m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_DOWNLOADING;
NPT_HttpEntity* entity;
NPT_InputStreamReference body;
if (!response ||
!(entity = response->GetEntity()) ||
NPT_FAILED(entity->GetInputStream(body)) ||
body.IsNull()) {
m_State = PLT_DOWNLOADER_ERROR;
NPT_LOG_WARNING_2("No body %d for %s", res, m_URL.ToString().GetChars());
return NPT_FAILURE;
}
// Read body (no content length means until socket is closed)
res = NPT_StreamToStreamCopy(*body.AsPointer(),
*m_Output.AsPointer(),
0,
entity->GetContentLength());
if (NPT_FAILED(res)) {
NPT_LOG_WARNING_2("Downloader error %d for %s", res, m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
NPT_LOG_INFO_1("Finished downloading %s", m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_SUCCESS;
return NPT_SUCCESS;
}
示例8: ShowResponse
/*----------------------------------------------------------------------
| ShowResponse
+---------------------------------------------------------------------*/
static void
ShowResponse(NPT_HttpResponse* response)
{
bool check_available = true;//true;
// show entity
NPT_HttpEntity* entity = response->GetEntity();
if (entity == NULL) return;
NPT_Console::OutputF("ENTITY: length=%lld, type=%s, encoding=%s\n",
entity->GetContentLength(),
entity->GetContentType().GetChars(),
entity->GetContentEncoding().GetChars());
NPT_DataBuffer buffer(65536);
NPT_Result result;
NPT_InputStreamReference input;
entity->GetInputStream(input);
NPT_TimeStamp start;
NPT_System::GetCurrentTimeStamp(start);
float total_read = 0.0f;
for (;;) {
NPT_Size bytes_read = 0;
NPT_LargeSize available = 0;
NPT_Size to_read = 65536;
if (check_available) {
input->GetAvailable(available);
if ((NPT_Size)available < to_read) to_read = (NPT_Size)available;
if (to_read == 0) {
to_read = 1;
NPT_TimeStamp sleep_time(0.01f);
NPT_System::Sleep(sleep_time);
}
}
result = input->Read(buffer.UseData(), to_read, &bytes_read);
if (NPT_FAILED(result)) break;
total_read += bytes_read;
NPT_TimeStamp now;
NPT_System::GetCurrentTimeStamp(now);
NPT_TimeStamp duration = now-start;
NPT_Console::OutputF("%6d avail, read %6d bytes, %6.3f KB/s\n", (int)available, bytes_read, (float)((total_read/1024.0)/(double)duration));
}
}
示例9: ReadBody
/*----------------------------------------------------------------------
| DumpBody
+---------------------------------------------------------------------*/
static NPT_Result
ReadBody(PLT_Downloader& downloader, NPT_InputStreamReference& stream, NPT_Size& size)
{
NPT_LargeSize avail;
char buffer[2048];
NPT_Result ret = NPT_ERROR_WOULD_BLOCK;
/* reset output param first */
size = 0;
/*
we test for availability first to avoid
getting stuck in Read forever in case blocking is true
and the download is done writing to the stream
*/
NPT_CHECK(stream->GetAvailable(avail));
if (avail) {
ret = stream->Read(buffer, 2048, &size);
NPT_LOG_FINER_2("Read %d bytes (result = %d)\n", size, ret);
return ret;
} else {
Plt_DowloaderState state = downloader.GetState();
switch (state) {
case PLT_DOWNLOADER_ERROR:
return NPT_FAILURE;
case PLT_DOWNLOADER_SUCCESS:
/* no more data expected */
return NPT_ERROR_EOS;
default:
NPT_System::Sleep(NPT_TimeInterval(0, 10000));
break;
}
}
return NPT_SUCCESS;
}
示例10: NPT_COMPILER_UNUSED
/*----------------------------------------------------------------------
| PLT_Downloader::ProcessResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_Downloader::ProcessResponse(NPT_Result res,
NPT_HttpRequest* request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse* response)
{
NPT_COMPILER_UNUSED(request);
NPT_COMPILER_UNUSED(context);
if (NPT_FAILED(res)) {
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_DOWNLOADING;
NPT_HttpEntity* entity;
NPT_InputStreamReference body;
if (!response || !(entity = response->GetEntity()) ||
NPT_FAILED(entity->GetInputStream(body)) || body.IsNull()) {
m_State = PLT_DOWNLOADER_ERROR;
return NPT_FAILURE;
}
// Read body (no content length means until socket is closed)
res = NPT_StreamToStreamCopy(*body.AsPointer(),
*m_Output.AsPointer(),
0,
entity->GetContentLength());
if (NPT_FAILED(res)) {
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_SUCCESS;
return NPT_SUCCESS;
}
示例11: serveFile
void serveFile(AbortableTask *task, const NPT_String& filePath, const NPT_String& mimeType, const FrontEnd::RequestContext& reqCtx, const NPT_HttpRequest *req, NPT_HttpResponse& resp, HttpOutput *httpOutput)
{
NPT_File f(filePath);
NPT_FileInfo fileInfo;
if (NPT_FAILED(f.GetInfo(fileInfo)) || fileInfo.m_Type != NPT_FileInfo::FILE_TYPE_REGULAR || NPT_FAILED(f.Open(NPT_FILE_OPEN_MODE_READ))) {
setStatusCode(resp, 404);
httpOutput->writeResponseHeader(resp);
return;
}
MediaStore::FileDetail detail;
detail.m_mimeType = mimeType;
detail.m_path = filePath;
detail.m_modificationTime = fileInfo.m_ModificationTime;
detail.m_size = fileInfo.m_Size;
detail.m_type = MediaStore::FileDetail::PosixFile;
NPT_InputStreamReference fileInput;
f.GetInputStream(fileInput);
AdvFileReader reader(fileInput.AsPointer());
serveStreamAdv(task, detail, &reader, reqCtx, req, resp, httpOutput, 1024 * 64);
}
示例12:
/*----------------------------------------------------------------------
| PLT_HttpHelper::GetBody
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpHelper::GetBody(const NPT_HttpMessage& message, NPT_String& body)
{
NPT_Result res;
NPT_InputStreamReference stream;
// get stream
NPT_HttpEntity* entity = message.GetEntity();
if (!entity ||
NPT_FAILED(entity->GetInputStream(stream)) ||
stream.IsNull()) {
return NPT_FAILURE;
}
// extract body
NPT_StringOutputStream* output_stream = new NPT_StringOutputStream(&body);
res = NPT_StreamToStreamCopy(*stream,
*output_stream,
0,
entity->GetContentLength());
delete output_stream;
return res;
}
示例13: file
/*----------------------------------------------------------------------
| PLT_FileMediaServer::OnAlbumArtRequest
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServer::OnAlbumArtRequest(NPT_String filepath, NPT_HttpResponse& response)
{
NPT_Size total_len;
NPT_File file(filepath);
NPT_InputStreamReference stream;
if (NPT_FAILED(file.Open(NPT_FILE_OPEN_MODE_READ)) || NPT_FAILED(file.GetInputStream(stream)) ||
NPT_FAILED(stream->GetSize(total_len)) || (total_len == 0)) {
goto filenotfound;
} else {
const char* extension = PLT_MediaItem::GetExtFromFilePath(filepath, m_DirDelimiter);
if (extension == NULL) {
goto filenotfound;
}
PLT_MetadataHandler* metadataHandler = NULL;
char* caData;
int caDataLen;
NPT_Result ret = NPT_ContainerFind(m_MetadataHandlers, PLT_MetadataHandlerFinder(extension), metadataHandler);
if (NPT_FAILED(ret) || metadataHandler == NULL) {
goto filenotfound;
}
// load the metadatahandler and read the cover art
if (NPT_FAILED(metadataHandler->Load(*stream)) || NPT_FAILED(metadataHandler->GetCoverArtData(caData, caDataLen))) {
goto filenotfound;
}
PLT_HttpHelper::SetContentType(&response, "application/octet-stream");
PLT_HttpHelper::SetBody(&response, caData, caDataLen);
delete caData;
return NPT_SUCCESS;
}
filenotfound:
response.SetStatus(404, "File Not Found");
return NPT_SUCCESS;
}
示例14: TcpServerLoop
/*----------------------------------------------------------------------
| TcpServerLoop
+---------------------------------------------------------------------*/
static void
TcpServerLoop(int port)
{
NPT_TcpServerSocket listener;
NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port));
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: Bind() failed (%d)\n", result);
}
NPT_Socket* client;
for (;;) {
NPT_Debug("waiting for client on port %d\n", port);
NPT_Result result = listener.WaitForNewClient(client);
NPT_SocketInfo socket_info;
client->GetInfo(socket_info);
NPT_Debug("client connected from %s:%d\n",
socket_info.remote_address.GetIpAddress().ToString().GetChars(),
socket_info.remote_address.GetPort());
NPT_InputStreamReference input;
client->GetInputStream(input);
NPT_OutputStreamReference output;
client->GetOutputStream(output);
do {
char buffer[1024];
NPT_Size bytes_read;
result = input->Read(buffer, sizeof(buffer), &bytes_read);
if (NPT_SUCCEEDED(result)) {
NPT_Debug("read %ld bytes\n", bytes_read);
output->Write(buffer, bytes_read);
}
} while (NPT_SUCCEEDED(result));
delete client;
}
}
示例15: main
//.........这里部分代码省略.........
current_endpoint->info.serial_port.speed = (unsigned int)speed;
} else {
NPT_Debug("ERROR: missing argument for 'serial' endpoint\n");
exit(1);
}
} else {
NPT_Debug("ERROR: invalid argument (%s)\n", arg);
exit(1);
}
if (current_endpoint == &in_endpoint) {
current_endpoint = &out_endpoint;
} else {
current_endpoint = NULL;
}
}
if (current_endpoint) {
NPT_Debug("ERROR: missing endpoint specification\n");
exit(1);
}
// data pump
NPT_Result result;
// allocate buffer
unsigned char* buffer;
buffer = (unsigned char*)malloc(packet_size);
if (buffer == NULL) {
NPT_Debug("ERROR: out of memory\n");
exit(1);
}
// get output stream
NPT_OutputStreamReference out;
result = GetEndPointStreams(&out_endpoint, NULL, &out);
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: failed to get stream for output (%d)", result);
exit(1);
}
unsigned long offset = 0;
unsigned long total = 0;
if (in_endpoint.type == ENDPOINT_TYPE_UDP_SERVER ||
in_endpoint.type == ENDPOINT_TYPE_MULTICAST_SERVER) {
NPT_UdpSocket* udp_socket;
result = GetEndPointUdpSocket(&in_endpoint, udp_socket);
// packet loop
NPT_DataBuffer packet(32768);
NPT_SocketAddress address;
do {
result = udp_socket->Receive(packet, &address);
if (NPT_SUCCEEDED(result)) {
if (Options.verbose) {
NPT_String ip = address.GetIpAddress().ToString();
NPT_Debug("Received %d bytes from %s\n", packet.GetDataSize(), ip.GetChars());
}
result = out->Write(packet.GetData(), packet.GetDataSize(), NULL);
offset += packet.GetDataSize();
total += packet.GetDataSize();
}
} while (NPT_SUCCEEDED(result));
} else {
// get the input stream
NPT_InputStreamReference in;
result = GetEndPointStreams(&in_endpoint, &in, NULL);
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: failed to get stream for input (%d)\n", result);
exit(1);
}
// stream loop
do {
NPT_Size bytes_read;
NPT_Size bytes_written;
// send
result = in->Read(buffer, packet_size, &bytes_read);
if (Options.show_progress) {
NPT_Debug("[%d]\r", total);
}
if (NPT_SUCCEEDED(result) && bytes_read) {
result = out->Write(buffer, bytes_read, &bytes_written);
if (Options.show_progress) {
NPT_Debug("[%d]\r", total);
}
offset += bytes_written;
total += bytes_written;
} else {
printf("[%d] *******************\n", result);
exit(1);
}
} while (NPT_SUCCEEDED(result));
}
delete buffer;
return 0;
}