Exemple #1
0
 public static async Task test_task2(FawRequest _req, FawResponse _res)
 {
     _res.write("hello world");
 }
Exemple #2
0
 public static Task test_task1(FawRequest _req, FawResponse _res)
 {
     _res.write("hello world");
     return(Task.CompletedTask);
 }
Exemple #3
0
        // processing a request
        private (bool, string, string) _request_http_once(Stream _req_stream, string _src_ip, CancellationToken _token, byte _first_byte)
        {
            bool   _static = true, _error = false;
            string _ws_module     = "";
            var    _request_begin = DateTime.Now;

            FawRequest  _req = new FawRequest();
            FawResponse _res = new FawResponse();

            try {
                _req.deserialize(_req_stream, _src_ip, _token, _first_byte);
                _req._check_int    = m_check_int;
                _req._check_long   = m_check_long;
                _req._check_string = m_check_string;

                if (_req.m_option == "HEAD" || _req.m_option == "OPTIONS")
                {
                    //throw new MyHttpException (204);
                    if (_req.m_option == "OPTIONS")
                    {
                        _res.m_headers.Add("Allow", "HEAD,GET,POST,PUT,DELETE,OPTIONS");
                    }
                }
                else if (_req.get_header("Upgrade").ToLower().IndexOf("websocket") >= 0)
                {
                    if (_req.get_header("Connection").ToLower() != "upgrade")
                    {
                        throw new MyHttpException(502);
                    }
                    var _ws_types = _req.get_header("Sec-WebSocket-Protocol").ToLower().Replace(" ", "").split_list(true, ',');
                    //if (_ws_types.IndexOf ("chat") == -1)
                    //	throw new MyHttpException (501);
                    if (_req.get_header("Sec-WebSocket-Version") != "13")
                    {
                        throw new MyHttpException(501);
                    }
                    _ws_module         = _req.m_path;
                    _res.m_status_code = 101;
                    _res.m_headers ["Sec-WebSocket-Accept"]  = $"{_req.get_header ("Sec-WebSocket-Key")}258EAFA5-E914-47DA-95CA-C5AB0DC85B11".to_bytes().sha1_encode().base64_encode();
                    _res.m_headers ["Sec-WebSocket-Version"] = "13";
                    _res.m_headers ["Upgrade"] = _req.get_header("Upgrade");
                    //_res.m_headers ["Origin"] = "";
                    //_res.m_headers ["Sec-WebSocket-Protocol"] = "chat.";
                    //_res.m_headers ["Sec-WebSocket-Extensions"] = "";
                    // TODO: check and reply
                }
                else if (!m_api_path.is_null() && _req.m_path.left_is(m_api_path.mid(1)))
                {
                    _static = false;
                    if (m_api_handlers.ContainsKey($"{_req.m_option} /{_req.m_path}"))
                    {
                        m_api_handlers [$"{_req.m_option} /{_req.m_path}"].process(_req, _res);
                    }
                    else if (m_api_handlers.ContainsKey($" /{_req.m_path}"))
                    {
                        m_api_handlers [$" /{_req.m_path}"].process(_req, _res);
                    }
                    else
                    {
                        throw new Exception("Unknown request");
                    }
                }
                else if (m_doc_info != null && !m_doc_path.is_null() && _req.m_path.left_is(m_doc_path.mid(1)))
                {
                    if (_req.m_path == $"{m_doc_path.mid (1)}api.json")
                    {
                        string _host = (_req.get_header("Host").is_null() ? $"127.0.0.1:{m_port}" : _req.get_header("Host"));
                        _res.write(m_swagger_data);
                        //_host = (_req.get_header ("Host").is_null () ? $"127.0.0.1:{m_port + 1}" : _req.get_header ("Host"));
                        //_res.write (m_swagger_data.Replace ("%-faq-host1-%", _host));
                        _res.set_content_from_filename(_req.m_path);
                    }
                    else if (m_doc_path != "/swagger/" && (_req.m_path == (m_doc_path.mid(1)) || _req.m_path.mid(m_doc_path.Length - 1) == "index.html"))
                    {
                        var _namespace = $"{MethodBase.GetCurrentMethod ().DeclaringType.Namespace}.Swagger.res.{_req.m_path.mid (m_doc_path.mid (1))}";
                        _namespace = (_namespace.right_is(".") ? $"{_namespace}index.html" : _namespace);
                        string _data = _read_from_namespace(_namespace).to_str();
                        _res.write(_data.Replace("/swagger/", m_doc_path));
                        _res.set_content_from_filename(_namespace);
                    }
                    else
                    {
                        var _namespace = $"{MethodBase.GetCurrentMethod ().DeclaringType.Namespace}.Swagger.res.{_req.m_path.mid (m_doc_path.mid (1))}";
                        _res.write(_read_from_namespace(_namespace));
                        _res.set_content_from_filename(_namespace);
                    }
                    //} else if (_req.m_path.left_is ("monitor/") && m_monitor != null) {
                    //	if (_req.m_path == "monitor/data.json") {
                    //		_res.write (m_monitor.get_json (_req.get_value<int> ("count", false)));
                    //		//_res.set_content_from_filename (_req.m_path);
                    //}
                }
                else if (_req.m_option == "GET")
                {
                    byte [] _data = _load_res(_req.m_path);
                    if (_data != null)
                    {
                        _res.write(_data);
                        _res.set_content_from_filename(_req.m_path.right_is("/") || _req.m_path.is_null() ? "index.html" : _req.m_path);
                    }
                    else
                    {
                        throw new MyHttpException(404);
                    }
                }
                else
                {
                    throw new MyHttpException(404);
                }
            } catch (MyHttpException ex) {
                if (ex.m_error_num == 404)
                {
                    if (_req.m_path == "404.html")
                    {
                        _res.m_status_code = ex.m_error_num;
                        _error             = (_res.m_status_code / 100 != 2);
                    }
                    else
                    {
                        _res.redirect("/404.html");
                    }
                }
                else
                {
                    _res.m_status_code = ex.m_error_num;
                    _error             = (_res.m_status_code / 100 != 2);
                }
            } catch (Exception ex) {
                _res.clear();
                _res.write(new { result = "failure", content = (ex.GetType().Name == "Exception" ? ex.Message : ex.ToString()) }.to_json());
                _res.m_status_code = 500;
                _error             = true;
            }
            //
            bool _go_ws = (!_ws_module.is_null()) && (!_error);

            _res.m_headers ["Connection"] = $"{(_go_ws ? "Upgrade" : "Keep-Alive")}";
            if (!_go_ws)
            {
                _res.m_headers ["Keep-Alive"] = $"timeout={(_go_ws ? 66 : 10)}, max=1000";
                if (!_res.m_headers.ContainsKey("Cache-Control"))
                {
                    _res.m_headers ["Cache-Control"] = "no-store";
                }
                if (!_res.m_headers.ContainsKey("Content-Type"))
                {
                    _res.m_headers ["Content-Type"] = "text/plain; charset=utf-8";
                }
            }
            _req_stream.Write(_res.build_response(_req, _go_ws));
            m_monitor?.OnRequest(_static, (long)((DateTime.Now - _request_begin).TotalMilliseconds + 0.5000001), _error);
            //
            string _api_key = "";

            if (_go_ws)
            {
                _api_key = _req.get_header("X-API-Key");
                if (_api_key.is_null() && _req.m_gets.ContainsKey("X-API-Key"))
                {
                    _api_key = _req.m_gets ["X-API-Key"];
                }
            }
            return(_go_ws, _ws_module, _api_key);
        }
Exemple #4
0
 public static void test_context(FawRequest _req, FawResponse _res)
 {
     _res.write("hello world");
 }
Exemple #5
0
 public static async Task test_task2(FawRequest _req, FawResponse _res)
 {
     _res.write("hello world");
     await Task.CompletedTask;
 }