Пример #1
0
PyObject * PyPreprocessor_scanHeaders( PyPreprocessor * self, PyObject * args, PyObject * kwds )
{
    static char * kwlist[] = { "pp_ctx", "filename", NULL };

    PyObject * pObject = 0;
    PyObject * filename = 0;

    assert( self->pp );

    if ( !PyArg_ParseTupleAndKeywords( args, kwds, "OO", kwlist, &pObject, &filename ) )
        return NULL;

    if ( !pObject || ( (PyTypeObject *)PyObject_Type( pObject ) != &PyPreprocessingContextType ) )
    {
        PyErr_SetString( PyExc_Exception, "Invalid preprocessing context parameter." );
        return NULL;
    }

    PyPreprocessingContext const * ppContext( reinterpret_cast<PyPreprocessingContext *>( pObject ) );

    if ( filename && !PyUnicode_Check( filename ) )
    {
        PyErr_SetString( PyExc_Exception, "Expected a string as 'filename' parameter." );
        return NULL;
    }

    Headers headers;
    HeaderList missing;
    PyThreadState * _save;
    bool result;
    try
    {
        Py_UNBLOCK_THREADS
        result = self->pp->scanHeaders( *ppContext->ppContext, PyUnicode_AsUTF8( filename ), headers, missing );
    }
    catch ( std::runtime_error const & error )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_RuntimeError, error.what() );
        return NULL;
    }
    catch ( std::exception const & error )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, error.what() );
        return NULL;
    }
    catch ( ... )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, "Unhandled exception" );
        return NULL;
    }
    
    if ( !result )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, "Failed to preprocess file." );
        return NULL;
    }

    // Group result by dir.
    typedef std::vector<Header const *> HeaderPtrList;
    typedef std::unordered_map<Dir, HeaderPtrList> DirsAndHeaders;
    DirsAndHeaders dirsAndHeaders;
    for ( Header const & header : headers )
        dirsAndHeaders[ header.dir ].push_back( &header );

    Py_BLOCK_THREADS
    PyObject * dirsTuple = PyTuple_New( dirsAndHeaders.size() );
    std::size_t dirIndex( 0 );
    for ( DirsAndHeaders::value_type const & dirAndHeaders : dirsAndHeaders )
    {
        PyObject * headersInDirTuple = PyTuple_New( dirAndHeaders.second.size() );
        std::size_t headersInDirTupleIndex( 0 );
        for ( Header const * header : dirAndHeaders.second )
        {
            PyObject * headerEntry = PyTuple_New( 3 );
            PyTuple_SET_ITEM( headerEntry, 0, PyUnicode_FromStringAndSize( header->name.get().data(), header->name.get().size() ) );

            PyObject * const isRelative( header->relative ? Py_True : Py_False );
            Py_INCREF( isRelative );
            PyTuple_SET_ITEM( headerEntry, 1, isRelative );
        
            PyContentEntry * contentEntry( (PyContentEntry *)_PyObject_New( &PyContentEntryType ) );
            contentEntry->ptr = header->contentEntry.get();
            intrusive_ptr_add_ref( contentEntry->ptr );

            PyTuple_SET_ITEM( headerEntry, 2, (PyObject *)contentEntry );
            PyTuple_SET_ITEM( headersInDirTuple, headersInDirTupleIndex++, headerEntry );
        }
        PyObject * dirTuple = PyTuple_New( 2 );
        llvm::StringRef const dirStr( dirAndHeaders.first.get() );
        PyObject * dir = PyUnicode_FromStringAndSize( dirStr.data(), dirStr.size() );
        PyTuple_SET_ITEM( dirTuple, 0, dir );
        PyTuple_SET_ITEM( dirTuple, 1, headersInDirTuple );
        PyTuple_SET_ITEM( dirsTuple, dirIndex++, dirTuple );
    }

    PyObject * missingHeadersTuple = PyTuple_New( missing.size() );
    std::size_t missingIndex( 0 );
    for ( HeaderList::value_type const & missingHeader : missing )
    {
        PyObject * val = PyUnicode_FromStringAndSize( missingHeader.data(), missingHeader.size() );
        PyTuple_SET_ITEM( missingHeadersTuple, missingIndex++, val );
    }
    
    PyObject * resultTuple = PyTuple_New( 2 );
    PyTuple_SET_ITEM( resultTuple, 0, dirsTuple );
    PyTuple_SET_ITEM( resultTuple, 1, missingHeadersTuple );
    return resultTuple;
}
Пример #2
0
    void CsvFile::read(HeaderList& header, DataMap& data) const  throw (FileException, CustomException)
    {
        std::ifstream file(filename.c_str()); 
        if (!file)  throw FileException("Cannot open file", filename, __FUNCTION_NAME__);
	
	
        // a buffer to store the line read
        std::string line;
	
        if ( ! std::getline(file, line))
            throw FileException("Cannot get the first line of file", filename, __FUNCTION_NAME__);
	

        unsigned int pointer = 0;
        unsigned int rawnb = 0;
        std::string word ="";
        int localHostColNumber = -1, tmp=0;
        bool uselocalhost=false;
        bool useotherthanlocalhost=false;
	
        // Parse the first line (header line)
        while(pointer < line.size())
            {
                while( pointer < line.size() && line[pointer] != separator)
                    {
                        //remove white spaces
                        if (line[pointer] != ' ')
                            {
                                word.push_back(line[pointer]);
                            }
                        pointer++;
                    }
                if( !word.empty())
                    {
                        // warning      if id    already in
                        if (std::find(header.begin(), header.end(), word) != header.end() )  
                            {
                                // Was error before, now just a warning: may be convenient  to have several time the same name in a header
                                Msg::warning("Duplicate header element '"+ word+"' in file "+filename, __FUNCTION_NAME__);
                                // throw FileException("Duplicate header elements'"+ word+"'",filename, __FUNCTION_NAME__);
                            }
                        header.push_back(word);
                        tmp++;
                     }
                // if we have "localhost" in csv file, note the column number.
                if(word == "localhost" || word == "Localhost" || word == "127.0.0.1")
                	localHostColNumber = tmp;

                 pointer++;
                word.clear();
            }
	  
        // Parse the lines
        while (std::getline(file, line))
            {
                rawnb++;
                pointer = 0;
                bool firstWord = true; // Are we parsing the first column
                unsigned int column = 0; // Which column is being scanned?
                std::string id;
                while(line.size() > pointer)
                    {	
                        // get word
                        word.clear();
                        while(line.size() > pointer && line[pointer] != separator)
                            {
                                if (line[pointer] != ' ')
                                    word.push_back(line[pointer]);
                                pointer++;
                            }
                        pointer++;
		  

                        if (firstWord)
                            {
                                // it's the map  id 
                                if ( word.empty() )  break; // forget about this line and go to parse next line
                                id = word;

                                if(data.find(id) != data.end())
                                	throw CustomException("Component : " + id + " is specified twice in CSV file.", __FUNCTION_NAME__ );

                                // init the raw of this id with zeros.
                                data[id].insert(data[id].begin(), header.size(),0);
                                firstWord = false;
                            }
                        else
                            {
                                if (!word.empty())
                                    {
                                	// store data
                                	try{
                                		data[id].at(column) = convertTo<int>(word);

                                		if (data[id].at(column) > 0)// one component instance at least mapped
                                		{
                                			if(column == localHostColNumber-1)   // we are reading in a locahost column
                                			{
                                				// Remember that we have one component mapped on a localHost.
                                				uselocalhost=true;
                                			}
                                			else
                                			{
                                				// Remember that we have one component mapped on an host different  from  localHost.
                                				useotherthanlocalhost=true;
                                			}
                                			if(uselocalhost && useotherthanlocalhost)
                                				throw FileException("Host name 'localhost' is used in CSV file in conjunction with other host names or IP address. Replace 'localhost' by IP address or machine name to avoid problems", filename,  __FUNCTION_NAME__);
                                		}
                                	}//end try
                                       catch(const BadConversion& e)
                                            {
                                                throw FileException("Raw="+toString<unsigned int>(rawnb+1)+", col="+toString<unsigned int>(column+2)+") contains non integer value="+word,filename, __FUNCTION_NAME__);
                                                //			      std::cerr << "ERROR: Csv File "<<fileName<<", element (raw="<<rawnb+1<<", col="<<column+2<<") contains non integer value="<<word<< std::endl;
                                            }
                                        catch(const std::out_of_range& e)
                                            {
                                                throw FileException("Element (raw="+toString<unsigned int>(rawnb+1)+", col="+toString<unsigned int>(column+2)+")  out of column  (value="+word+", ignored)",filename, __FUNCTION_NAME__);
                                                //			      std::cerr << "WARNING : Csv File "<<fileName<<", element (raw="<<rawnb+1<<", col="<<column+2<<")  out of column  (value="<<word<<", ignored)"<< std::endl;
                                            }

                                    }
                                column++;
                            }
                    }
            }
    };
Пример #3
0
static ngx_int_t ngx_http_cxxmvc_handler(ngx_http_request_t *r)
{
	using dragon::kHttp_Method_Get;
	using dragon::kResponseTypeRedirect;
	using dragon::HeaderList;
	using dragon::CookieList;
	using dragon::StringRef;
	using dragon::HttpRequest;
	using dragon::HttpResponse;

	ngx_buf_t *buf = NULL;
	ngx_chain_t out;

	unsigned uriLen = r->uri.len;
	if (r->args.len > 0) 
		uriLen = r->uri.len + r->args.len + 1;
	
	struct sockaddr_in *sin;
	ngx_addr_t          addr;
	addr.sockaddr = r->connection->sockaddr;
	addr.socklen  = r->connection->socklen;
	sin = (struct sockaddr_in *)addr.sockaddr;
	char *ip = inet_ntoa(sin->sin_addr);

	StringRef IP(ip, strlen(ip));
	StringRef routingUrl((const char *)r->uri.data, uriLen);
	StringRef userAgent;
	if (r->headers_in.user_agent)
		userAgent = StringRef((const char *)r->headers_in.user_agent->value.data, r->headers_in.user_agent->value.len);
	
	std::string userCookie = ngx_http_get_cookie(r);
	std::string lang       = ngx_http_get_language(r);

	HttpRequest  req;
	HttpResponse res;

	req.SetMethod(kHttp_Method_Get);
	req.SetUrl(routingUrl);
	req.SetIp(IP);
	//req.SetHost(StringRef(host, strlen(host)));
	req.SetUserAgent(userAgent);
	req.SetUserCookie(StringRef(userCookie.c_str(), userCookie.length()));
	req.SetAcceptLanguage(StringRef(lang.c_str(), lang.length()));
        req.ParseCookie();

	DE.ResponseRequest(req, res);

	ngx_str_t k = ngx_string("X-Powered-By");
	ngx_str_t v = ngx_string("cxxmvc/0.1");;
	ngx_http_add_header(r, &v, &k);

	if (res.GetResponseType() == kResponseTypeRedirect)
	{
		ngx_str_t k = ngx_string("location");
		ngx_str_t v = {res.GetContent().length(), (u_char *)res.GetContent().c_str()};
		ngx_http_add_header(r, &v, &k);
	}

	HeaderList headers = res.GetHeaders();
	if (headers.size() > 0) 
	{
		HeaderList::iterator iter;
		HeaderList::iterator end = headers.end();
		for (iter = headers.begin(); iter != end; ++iter)
		{		
			ngx_str_t k;
			ngx_str_t v; 

			k.data = (u_char *)ngx_pcalloc(r->pool, iter->first.length()+1);
			k.len  = iter->first.length();
			strcpy((char *)k.data, (const char *)iter->first.c_str());

			v.data = (u_char *)ngx_pcalloc(r->pool, iter->second.length()+1);
			v.len  = iter->second.length();
			strcpy((char *)v.data, (const char *)iter->second.c_str());

		//	std::cout << "key : "<< k.data << std::endl;
		//	std::cout << "value : " << v.data << std::endl;

			ngx_http_add_header(r, &v, &k);
		}
	}

	CookieList cookies = res.GetCookies();
	if (cookies.size() > 0)
	{
		CookieList::iterator iter;
		CookieList::iterator end = cookies.end();
		for (iter = cookies.begin(); iter != end; ++iter)
		{
			ngx_str_t v; 

			v.data = (u_char *)ngx_pcalloc(r->pool, iter->length()+1);
			v.len  = iter->length();
			strcpy((char *)v.data, (const char *)iter->c_str());

			ngx_http_add_cookie(r, v);
		}
	}

	r->headers_out.status             = res.GetStatusCode();
    	r->headers_out.content_type.len   = res.GetContentType().length();
	r->headers_out.content_type.data  = (u_char *)res.GetContentType().c_str();

	if (res.GetDetaRef().length() > 0)
		r->headers_out.content_length_n = res.GetDetaRef().length();
        else 
       		r->headers_out.content_length_n = res.GetContent().length();


        ngx_http_send_header(r);

        buf = (ngx_buf_t *)ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

        if (buf == NULL) {
                ngx_log_error(NGX_LOG_ERR,
                              r->connection->log,
                              0,
                              "Failed to allocate response buffer.");
                return NGX_ERROR;
        }

	if (res.GetDetaRef().length() > 0) {
		buf->pos = (u_char *)res.GetDetaRef().data();
       		buf->last = buf->pos + res.GetDetaRef().length();
	} else {
       		buf->pos = (u_char *)res.GetContent().c_str();
        	buf->last = buf->pos + res.GetContent().length();
	}
        buf->memory   = 1; /* content is in read-only memory */
        buf->last_buf = 1; /* there will be no more buffers in the request */

	out.buf    = buf;
	out.next   = NULL;

	return ngx_http_output_filter(r, &out);
}