Example #1
0
Ref<FetchProtocol>
SourceManager::getFetchProtocolForUri (ConstMemory const uri)
{
    ConstMemory protocol_name;
    {
        Count i = 0;
        for (Count const i_end = uri.len(); i < i_end; ++i) {
            if (uri.mem() [i] == ':')
                break;
        }
        protocol_name = uri.region (0, i);
    }

    Ref<FetchProtocol> fetch_protocol;
    {
        mutex.lock ();
        FetchProtocolHash::EntryKey const fetch_protocol_key = fetch_protocol_hash.lookup (protocol_name);
        if (fetch_protocol_key) {
            fetch_protocol = fetch_protocol_key.getData();
        }
        mutex.unlock ();
    }

    if (!fetch_protocol) {
        logE_ (_func, "Fetch protocol not found: ", protocol_name);
        return NULL;
    }

    return fetch_protocol;
}
mt_throws AsyncIoResult
NativeAsyncFile::write (ConstMemory   const mem,
                        Size        * const ret_nwritten)
{
    if (ret_nwritten)
	*ret_nwritten = 0;

    // According to POSIX, if we pass a value larger than SSIZE_MAX to read,
    // then the result is implementation-defined.
    Size len;
    if (mem.len() > SSIZE_MAX)
	len = SSIZE_MAX;
    else
	len = mem.len();

    ssize_t const res = ::write (fd, mem.mem(), len);

    if (res == -1) {
	if (errno == EAGAIN || errno == EWOULDBLOCK) {
	    requestOutput ();
	    return AsyncIoResult::Again;
	}

	if (errno == EINTR)
	    return AsyncIoResult::Normal;

	if (errno == EPIPE) {
	  // If there'll be a need to distinguish Eof from Error, then this
	  // is the place to intervene.
	    return AsyncIoResult::Error;
	}

	exc_throw (PosixException, errno);
	exc_push_ (IoException);
	return AsyncIoResult::Error;
    } else
    if (res < 0) {
	exc_throw (InternalException, InternalException::BackendMalfunction);
	return AsyncIoResult::Error;
    }

    if ((Size) res > len) {
	exc_throw (InternalException, InternalException::BackendMalfunction);
	return AsyncIoResult::Error;
    }

    if (ret_nwritten)
	*ret_nwritten = res;

    return AsyncIoResult::Normal;
}
mt_throws Result
AndroidLogcatOutputStream::write (ConstMemory   const mem,
                                  Size        * const ret_nwritten)
{
    if (ret_nwritten)
        *ret_nwritten = mem.len();

    __android_log_print (ANDROID_LOG_INFO,
                         "---DEBUG",
                         "%.*s",
                         (int) mem.len(),
                         (char const *) mem.buf());

    return Result::Success;
}
Example #4
0
BooleanValue strToBoolean (ConstMemory const value_mem)
{
    if (value_mem.len() == 0)
	return Boolean_Default;

    // TODO malloc here is not necessary
    Ref<String> value_str = grab (new String (value_mem));
    Memory const mem = value_str->mem();
    for (Size i = 0; i < mem.len(); ++i)
	mem.mem() [i] = (Byte) tolower (mem.mem() [i]);

    if (equal (mem, "y")    ||
	equal (mem, "yes")  ||
	equal (mem, "on")   ||
	equal (mem, "true") ||
	equal (mem, "1"))
    {
	return Boolean_True;
    }

    if (equal (mem, "n")     ||
	equal (mem, "no")    ||
	equal (mem, "off")   ||
	equal (mem, "false") ||
	equal (mem, "0"))
    {
	return Boolean_False;
    }

    return Boolean_Invalid;
}
Example #5
0
static bool
whitespaceBetweenWordsNeeded (ConstMemory const &left,
			      ConstMemory const &right)
{
    if (left .len() == 0 ||
	right.len() == 0)
    {
	return false;
    }

    if (isalnum (left.mem() [left.len() - 1]) &&
	isalnum (right.mem() [0]))
    {
	return true;
    }

    return false;
}
Example #6
0
void
HttpRequest::parseParameters_paramCallback (ConstMemory   const name,
                                            ConstMemory   const value,
                                            void        * const _self)
{
    HttpRequest * const self = static_cast <HttpRequest*> (_self);

    HttpRequest::Parameter * const param = new (std::nothrow) HttpRequest::Parameter;
    assert (param);
    param->name = name;
    param->value = value;

    self->parameter_hash.add (param);

    //** unescape parsed parameter
    std::string key ((const char*) name.mem(), name.len());
    if(value.len() == 0)
    {
        self->decoded_args[key] = std::string("true");
        return;
    }
    std::string undecoded_value((const char*)value.mem());

    size_t req_end = undecoded_value.find(" HTTP");
    size_t param_end = undecoded_value.find_first_of('&');

    size_t end;

    if (param_end > 0 && req_end == -1)
        end = param_end;
    else if (param_end == -1 && req_end > 0)
        end = req_end;
    else if (param_end > 0 && req_end > 0)
        end = std::min (param_end, req_end);
    else 
        end = undecoded_value.length();

    std::string decoded_value;
    undecoded_value = undecoded_value.substr(0, end);

    url_decode(undecoded_value, decoded_value);

    self->decoded_args[key] = decoded_value;
}
Example #7
0
BooleanValue strToBoolean (ConstMemory const value_mem)
{
    if (value_mem.len() == 0)
        return Boolean_Default;

    Byte arr [32];
    Memory mem;
    {
        Size len = sizeof (arr);
        if (len > value_mem.len())
            len = value_mem.len();

        memcpy (arr, value_mem.buf(), len);
        mem = Memory (arr, len);
    }

    for (Size i = 0; i < mem.len(); ++i)
        mem.mem() [i] = (Byte) tolower (mem.mem() [i]);

    if (equal (mem, "y")    ||
            equal (mem, "yes")  ||
            equal (mem, "on")   ||
            equal (mem, "true") ||
            equal (mem, "1"))
    {
        return Boolean_True;
    }

    if (equal (mem, "n")     ||
            equal (mem, "no")    ||
            equal (mem, "off")   ||
            equal (mem, "false") ||
            equal (mem, "0"))
    {
        return Boolean_False;
    }

    return Boolean_Invalid;
}
Example #8
0
void _libMary_unreachable_ (ConstMemory const &mem,
                            ConstMemory const &func)
{
    fprintf (stderr, "%.*s%.*s: unreachable point reached\n", (int) mem.len(), mem.mem(), (int) func.len(), func.mem());
    abort ();
}
Example #9
0
    receiver->setFrontend (CbDesc<Receiver::Frontend> (&receiver_frontend, this, this));
}

LineServer::LineServer (EmbedContainer * const embed_container)
    : Object (embed_container),
      max_line_len (4096),
      recv_pos (0)
{
}

LineServer::~LineServer ()
{
}

ConstMemory lineEscape (ConstMemory     const line,
                        StRef<String> * const mt_nonnull ret_ref)
{
    Byte const * const buf = line.buf();
    Size const len = line.len();
    Count esc_len = len;
    bool esc = false;
    for (Size i = 0; i < len; ++i) {
        if (buf [i] == ' ') {
            esc = true;
        } else
        if (buf [i] == '_' || buf [i] == '\\') {
            ++esc_len;
            esc = true;
        }
    }
Example #10
0
mt_sync_domain (readTask) bool
GetFileSession::readTask (void * const _self)
{
    GetFileSession * const self = static_cast <GetFileSession*> (_self);

    logD (getfile, _func_);

    if (self->session_state == SessionState_Header) {
        MOMENT_SERVER__HEADERS_DATE

        for (;;) {
            MediaReader::ReadFrameResult const res = self->media_reader->readMoreData (&read_frame_backend, self);
            if (res == MediaReader::ReadFrameResult_Failure) {
                logE_ (_func, "ReadFrameResult_Failure");

                ConstMemory msg = "Data retrieval error";
                self->sender->send (self->page_pool,
                                    true /* do_flush */,
                                    // TODO No cache
                                    MOMENT_SERVER__500_HEADERS (msg.len()),
                                    "\r\n",
                                    msg);

                if (!self->req_is_keepalive)
                    self->sender->closeAfterFlush ();

                logA_ ("mod_nvr 500 ", self->req_client_addr, " ", self->req_request_line);
                return false /* do not reschedule */;
            }

            bool header_done = false;
            if (res == MediaReader::ReadFrameResult_NoData) {
                logD (getfile, _func, "ReadFrameResult_NoData");

                if (!self->got_last_audio_ts &&
                    !self->got_last_video_ts)
                {
                    ConstMemory msg = "Requested video data not found";
                    self->sender->send (self->page_pool,
                                        true /* do_flush */,
                                        // TODO No cache
                                        MOMENT_SERVER__404_HEADERS (msg.len()),
                                        "\r\n",
                                        msg);

                    if (!self->req_is_keepalive)
                        self->sender->closeAfterFlush ();

                    logA_ ("mod_nvr 404 ", self->req_client_addr, " ", self->req_request_line);
                    return false /* do not reschedule */;
                }

                header_done = true;
            } else
            if (res == MediaReader::ReadFrameResult_Finish) {
                logD (getfile, _func, "ReadFrameResult_Finish");
                header_done = true;
            }

            if (header_done) {
                self->session_state = SessionState_Data;
                self->media_reader->reset ();
                break;
            }

            assert (res != MediaReader::ReadFrameResult_BurstLimit);
            assert (res == MediaReader::ReadFrameResult_Success);
        }

        PagePool::PageListInfo const mp4_header = self->mp4_muxer.pass1_complete (self->duration_sec * 1000);

        {
            self->sender->send (self->page_pool,
                                true /* do_flush */,
                                // TODO No cache
                                MOMENT_SERVER__OK_HEADERS (
                                        (!self->octet_stream_mime ? ConstMemory ("video/mp4") :
                                                                    ConstMemory ("application/octet-stream")),
                                        mp4_header.data_len + self->mp4_muxer.getTotalDataSize()),
                                "\r\n");
            logD_ (_func, "CONTENT-LENGTH: ", mp4_header.data_len + self->mp4_muxer.getTotalDataSize());

            if (!self->req_is_keepalive)
                self->sender->closeAfterFlush ();

            logA_ ("mod_nvr 200 ", self->req_client_addr, " ", self->req_request_line);
        }

        {
            SenderMessageEntry_Pages * const msg_pages = SenderMessageEntry_Pages::createNew (/*header_len=*/ 0);
            msg_pages->init (mp4_header.first, self->page_pool, /*msg_offs=*/ 0, mp4_header.data_len);

            self->sender->sendMessage (msg_pages, true /* do_flush */);
        }

        self->transfer_start_time_millisec = getTimeMilliseconds();
        self->bytes_transferred += mp4_header.data_len;

        self->sender->getEventInformer()->subscribe (
                CbDesc<Sender::Frontend> (&sender_frontend, self, self));
    }
Example #11
0
void
PagePool::PageListArray::set (Size        const offset,
                              ConstMemory const mem)
{
    doGetSet (offset, NULL /* data_get */, mem.mem() /* data_set */, mem.len(), false /* get */);
}