public void AddSession(EditSession session) { lock (_syncObj) { _sessions.Add(session.SessionId, session); } }
public void DelSession(EditSession session) { lock (_syncObj) { // Clean up session.Dispose(); _sessions.Remove(session.SessionId); } }
private void ProcessRequest(IAsyncResult result) { try { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); Console.WriteLine(DateTime.Now.ToString() + " : " + context.Request.Url); try { var access_token = context.Request.QueryString["access_token"]; var path = context.Request.Url.AbsolutePath; //虚拟路径 var newpath = path.Replace("wopi/files", "^").Split('^')[1].Replace("/contents", ""); var sessionid = ConfigManager.MD5(newpath); EditSession editSession = EditSessionManager.Instance.GetSession(sessionid); //物理路径 var fullfile = config.root.TrimEnd('/') + newpath; //文件存在 if (File.Exists(fullfile)) { if (editSession == null) { editSession = new FileSession(sessionid, fullfile, config.login, config.name, config.mail, false); EditSessionManager.Instance.AddSession(editSession); } } else { Console.WriteLine(@"Invalid request"); ErrorResponse(context, @"Invalid request parameter"); m_listener.BeginGetContext(ProcessRequest, m_listener); return; } if (path.IndexOf(@"/contents") == -1 && context.Request.HttpMethod.Equals(@"GET")) { //request of checkfileinfo, will be called first var memoryStream = new MemoryStream(); var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, editSession.GetCheckFileInfo()); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.Close(); } else if (path.IndexOf(@"/contents") >= 0) { // get and put file's content if (context.Request.HttpMethod.Equals(@"POST")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); editSession.Save(ms.ToArray()); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; } else { var content = editSession.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; context.Response.OutputStream.Write(content, 0, content.Length); } context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && (context.Request.Headers["X-WOPI-Override"].Equals("LOCK") || context.Request.Headers["X-WOPI-Override"].Equals("UNLOCK") || context.Request.Headers["X-WOPI-Override"].Equals("REFRESH_LOCK")) ) { //lock, Console.WriteLine("request lock: " + context.Request.Headers["X-WOPI-Override"]); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else { Console.WriteLine(@"Invalid request parameters"); ErrorResponse(context, @"Invalid request cobalt parameter"); } } catch (Exception ex) { Console.WriteLine(@"process request exception:" + ex.Message); } m_listener.BeginGetContext(ProcessRequest, m_listener); } catch (Exception ex) { Console.WriteLine(@"get request context:" + ex.Message); return; } }