Ejemplo n.º 1
0
        // static http request proccessing logic
        public async Task <IgniteResponse> proccess(IgniteRequest request)
        {
            // get resource name from route
            String route = request.getRoute();

            String[] routePath = route.Split("/");
            if (route == "/")
            {
                logger.debug("StaticHttpProccessor@proccess | default route, servinc default page {0}", DEFAULT_PATH);
                route = DEFAULT_PATH;
            }
            else if (routePath[routePath.Length - 1].Split(".").Length == 1)
            {
                // if last path part don't have extension add default
                logger.debug("StaticHttpProccessor@proccess | no extension, adding default", DEFAULT_EXTENSION);
                route = route + DEFAULT_EXTENSION;
            }

            // reverse slashes ;)
            route = route.Replace("/", "\\");
            logger.info("StaticHttpProccessor@proccess | route {0}", route);

            // get configured working directory from configuration file
            ConfigurationService configService = await ConfigurationService.getInstance();

            IgniteConfiguration config = configService.getCoreServerConfig();
            //Console.WriteLine("StaticHttpProccessor@proccess | config {0}", config);
            String staticDataDir = config.getProperty(STATIC_DIR_PROP_NAME);

            // get file by filename
            try {
                var file = await FileSystemService.readFullFile(staticDataDir + route);

                logger.debug("StatichttpProccessor@proccess | file finded", file);
                IgniteResponse response = IgniteResponseFactory.getInstance();
                response.setBody(file);

                // set MIME TYPE
                String MIME = MIMEType.getMIMETypeByExtension(getFileExtensionByRoute(route));
                response.getHeaders()[HttpHeaders.ContentLength] = " " + file.Length.ToString();
                response.getHeaders()[HttpHeaders.ContentType]   = " " + MIME;

                return(response);
            } catch (FileNotFoundException e) {
                logger.warn("StaticHttpProccessor@proccess | no file finded by path {0}, reutning 404", staticDataDir + route);
                return(IgniteResponseFactory.getInstance(new IgniteResponseStatus(HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND_MESSAGE)));
            }
        }
Ejemplo n.º 2
0
    public async static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;
        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state   = (StateObject)ar.AsyncState;
        Socket      handler = state.workSocket;

        // Read data from the client socket.
        int bytesRead = handler.EndReceive(ar);

        //Console.WriteLine("AsyncEngine@ReadCallback | readed bytes {0}", bytesRead);

        // catch every exception on server and send 500 if exception occured
        try  {
            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                content = state.sb.ToString();
                //Console.WriteLine("Content | {0}", content);

                // if readed bytes >= BUFFER_SIZE -> read more bytes
                if (bytesRead != 1024)
                {
                    // All the data has been read from the
                    // client. Display it on the console.


                    logger.info("AsyncEngine@ReadCallback | Read {0} bytes from socket",
                                content.Length);
                    logger.info("AsyncEngine@ReadCallback | Raw Request \n{0}", content);



                    IgniteRequest        request = requestParser.Parse(content);
                    IgniteResponseStatus status  = IgniteRequestValidatorService.validate(request);

                    if (status.getCode() != HttpStatus.OK)
                    {
                        logger.warn("AsyncEngine@ReadCallback | request not valid {0}:{1}", status.getCode(), status.getMessage());
                        IgniteResponse failedResponse = IgniteResponseFactory.getInstance(status);
                        String         rawResponse    = responseParser.stringify(failedResponse);

                        logger.info("\n\nAsyncEngine@ReadCallback | raw response \n {0}", rawResponse);

                        Send(handler, rawResponse);
                    }
                    else
                    {
                        //Console.WriteLine("AsyncEngine@ReadCallback | request valid");
                        //Console.WriteLine("AsyncEngine@ReadCallback | Parsed request {0}", request);
                        Proccessor     proccessor = ProccessorFactory.getInstance();
                        IgniteResponse response   = await proccessor.proccess(request);

                        String rawResponse = responseParser.stringify(response);

                        logger.info("AsyncEngine@ReadCallback | raw response \n{0}", rawResponse);

                        Send(handler, rawResponse);
                    }
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                         new AsyncCallback(ReadCallback), state);
                }
            }
        } catch (Exception e) {
            logger.error("AsyncEngine@ReadCallback | Server erorr occured {0}", e);
            IgniteResponse failedResponse = IgniteResponseFactory.getInstance(new IgniteResponseStatus(HttpStatus.SERVER_ERROR, HttpStatus.SERVER_ERROR_MESSAGE));
            String         rawResponse    = responseParser.stringify(failedResponse);

            logger.info("AsyncEngine@ReadCallback | raw response \n", rawResponse);


            Send(handler, rawResponse);
        }
    }