public byte [] build_response(FawRequest _req, bool _upgrade_ws = false) { var _data = new List <byte> (); _data.AddRange($"{_req.m_version} {m_status_code} {(m_codes.ContainsKey (m_status_code) ? m_codes [m_status_code] : "Unknown Error")}\r\n".to_bytes()); // string [] _encodings = (_req.m_headers.ContainsKey("Accept-Encoding") ? _req.m_headers ["Accept-Encoding"].Split(',') : new string [0]); _encodings = (from p in _encodings select p.Trim().ToLower()).ToArray(); var _cnt_data = m_data; if (Array.IndexOf(_encodings, "gzip") >= 0 && !_upgrade_ws) { m_headers ["Content-Encoding"] = "gzip"; m_headers ["Vary"] = "Accept-Encoding"; _cnt_data = m_data.gzip_compress(); } else if (Array.IndexOf(_encodings, "deflate") >= 0 && !_upgrade_ws) { m_headers ["Content-Encoding"] = "deflate"; m_headers ["Vary"] = "Accept-Encoding"; _cnt_data = m_data.deflate_compress(); } m_headers ["Content-Length"] = _cnt_data.Count.to_str(); foreach (var(_key, _val) in m_headers) { _data.AddRange($"{_format_key (_key)}: {_val}\r\n".to_bytes()); } _data.AddRange("\r\n".to_bytes()); //if (m_status_code != 200 && m_status_code != 101 && _cnt_data.Count == 0) { // _data.AddRange (m_codes [m_status_code].to_bytes ()); //} else { // _data.AddRange (_cnt_data); //} _data.AddRange(_cnt_data); return(_data.ToArray()); }
public void process(FawRequest _req, FawResponse _res) { try { object _obj = null; if (!m_method.IsStatic) { var _jwt_obj = JWTManager.Check(_req.get_header("X-API-Key")); _obj = m_auth_method.Invoke(null, new object [] { _jwt_obj }); } // var _params = new object [m_params.Count]; for (int i = 0; i < m_params.Count; ++i) { if (m_params [i].Item1 != null) { _params [i] = _req.get_type_value(m_params [i].Item1, m_params [i].Item2); } else { switch (m_params [i].Item2) { case "FawRequest": _params [i] = _req; break; case "FawResponse": _params [i] = _res; break; case ":IP": _params [i] = _req.m_ip; break; case ":AgentIP": _params [i] = _req.m_agent_ip; break; case ":Option": _params [i] = _req.m_option; break; default: throw new Exception($"{m_name}: Request parameter types that are not currently supported"); } } } var _ret = m_method.Invoke(_obj, _params); //if (_ret.GetType () == typeof (Task<>)) // 始终为False if (_ret is Task _t) { if (_ret?.GetType() != typeof(Task)) { //_ret = _ret.GetType ().InvokeMember ("Result", BindingFlags.GetProperty, null, _ret, null); _ret = (_ret as dynamic).Result; } else { _t.Wait(); _ret = null; } } if (m_jwt_type == "Gen") { var(_o, _exp) = ((object, DateTime))_ret; _ret = JWTManager.Generate(_o, _exp); } if (!_res.has_data()) { if (_ret != null) { if (_ret is byte _byte) { _res.write(_byte); } else if (_ret is byte [] _bytes) { _res.write(_bytes); } else { string _content = (_ret.GetType().IsPrimitive ? _ret.to_str() : _ret.to_json()); object _o; if (_content == "") { _o = new { result = "success" }; } else if (_content[0] != '[' && _content[0] != '{') { _o = new { result = "success", content = _content }; } else { _o = new { result = "success", content = JToken.Parse(_content) }; } _res.write(_o.to_json()); } } else { _res.write(new { result = "success" }.to_json()); } } } catch (TargetInvocationException ex) { if (ex.InnerException != null && ex.InnerException?.GetType() == typeof(Exception)) { throw ex.InnerException; } throw ex; } }