using System.Globalization;
using System.Web.Hosting;
using vmw.common;
-using vmw.@internal.j2ee;
using InputStream=java.io.InputStream;
+using java.security;
namespace Mainsoft.Web.Hosting {
[MonoTODO("Implement security demands on the path usage functions (and review)")]
[ComVisible (false)]
- internal sealed class ServletWorkerRequest : HttpWorkerRequest, IServiceProvider {
- readonly HttpServlet _HttpServlet;
- readonly HttpServletRequest _HttpServletRequest;
- readonly HttpServletResponse _HttpServletResponse;
- java.io.OutputStream _OutputStream;
+ public abstract class BaseWorkerRequest : HttpWorkerRequest, IHttpExtendedWorkerRequest, IServiceProvider
+ {
+ OutputStreamWrapper _OutputStream;
+ readonly string _contextPath;
readonly string _requestUri;
readonly string _pathInfo;
static readonly Hashtable KnownServerVariableMap;
- static ServletWorkerRequest() {
+ static BaseWorkerRequest() {
_srvVarsToHeaderMap = new StringDictionary();
_srvVarsToHeaderMap.Add("HTTP_ACCEPT", "Accept");
_srvVarsToHeaderMap.Add("HTTP_REFERER", "Referer");
KnownServerVariableMap[knownServerVariableNames[i]] = (KnownServerVariable)i;
}
- public ServletWorkerRequest (HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp, java.io.OutputStream outputStream) {
- _HttpServlet = servlet;
- _HttpServletRequest = req;
- _HttpServletResponse = resp;
- _OutputStream = outputStream;
+ public BaseWorkerRequest (string contextPath, string servletPath, string requestURI) {
- string contextPath = req.getContextPath();
- string requestURI = contextPath + req.getServletPath();
+
+ _contextPath = contextPath;
+
+ //string contextPath = req.getContextPath();
+ //string servletPath = req.getServletPath ();
+ //string requestURI = req.getRequestURI ();
+ // servletPath - Returns the part of this request's URL that calls the servlet.
+ // so it contains default page.
+ // requestURI - Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
+ // so it contains what the user passed.
+ //
+ // the one containing more information wins.
+ if (contextPath.Length + servletPath.Length > requestURI.Length)
+ requestURI = contextPath + servletPath;
+ else {
+ int contextPos = requestURI.IndexOf(contextPath, StringComparison.Ordinal);
+ if (contextPos > 0)
+ requestURI = requestURI.Substring (contextPos);
+ }
_requestUri = Uri.UnescapeDataString(requestURI);
const int dotInvokeLength = 7; //".invoke".Length
int paramNameStart = _requestUri.LastIndexOf('/');
_pathInfo = _requestUri.Substring(paramNameStart, _requestUri.Length - paramNameStart);
}
- }
- public object GetService (Type serviceType)
- {
- if (serviceType == typeof(HttpServlet))
- return Servlet;
- if (serviceType == typeof(HttpServletRequest))
- return ServletRequest;
- if (serviceType == typeof(HttpServletResponse))
- return ServletResponse;
- return null;
- }
+ const int aspnetconfigLength = 12; //"aspnetconfig".Length
+ int endingSlash = _requestUri [_requestUri.Length - 1] == '/' ? 1 : 0;
+ if (_requestUri.Length > aspnetconfigLength &&
+ String.CompareOrdinal ("aspnetconfig", 0, _requestUri,
+ _requestUri.Length - aspnetconfigLength - endingSlash, aspnetconfigLength) == 0) {
- public HttpServlet Servlet {
- get {
- return _HttpServlet;
- }
- }
-
- public HttpServletRequest ServletRequest {
- get{
- return _HttpServletRequest;
+ if (endingSlash == 0)
+ _requestUri += "/";
+ _requestUri += "Default.aspx";
}
}
- public HttpServletResponse ServletResponse {
- get{
- return _HttpServletResponse;
- }
+ static readonly Type typeOfWriter = typeof (java.io.Writer);
+ public virtual object GetService (Type serviceType)
+ {
+ if (serviceType == typeOfWriter)
+ return CreateOutputStream (false);
+ return null;
}
[MonoTODO("Implement security")]
}
public override void FlushResponse (bool finalFlush) {
- IPortletActionResponse resp =_HttpServletResponse as IPortletActionResponse;
- if (_OutputStream == null || resp != null && resp.isRedirected())
+ //kostat
+ //IPortletActionResponse resp =_HttpServletResponse as IPortletActionResponse;
+ //if (_OutputStream == null || resp != null && resp.isRedirected())
+ // return;
+ if (_OutputStream == null)
return;
_OutputStream.flush();
}
public override string GetAppPath () {
- return _HttpServletRequest.getContextPath();
- }
- public override string GetAppPathTranslated () {
- return J2EEUtils.GetApplicationRealPath(_HttpServlet.getServletConfig());;
+ return _contextPath;
}
public override string GetFilePath () {
string uri = GetUriPath();
string pathInfo = GetPathInfo();
if (pathInfo != null && pathInfo.Length > 0)
- uri = uri.Substring(0, uri.Length - pathInfo.Length);
+ uri = uri.Substring (0, uri.Length - pathInfo.Length);
return uri;
}
return Path.Combine (GetAppPathTranslated (), page);
}
- public override string GetHttpVerbName () {
- return _HttpServletRequest.getMethod();
- }
-
- public override string GetHttpVersion () {
- return _HttpServletRequest.getProtocol();
- }
-
- public override string GetLocalAddress () {
- return _HttpServletRequest.getLocalAddr();
- }
-
- public override int GetLocalPort () {
- return _HttpServletRequest.getServerPort();
- }
-
public override string GetPathInfo () {
- string pathInfo = _pathInfo != null ? _pathInfo : _HttpServletRequest.getPathInfo();
- return pathInfo != null ? pathInfo : String.Empty;
- }
-
- public override string GetQueryString () {
- return _HttpServletRequest.getQueryString();
- }
+ return _pathInfo;
+ }
public override string GetRawUrl () {
if (_rawUrl == null) {
return _rawUrl;
}
- public override string GetRemoteAddress() {
- return _HttpServletRequest.getRemoteAddr();
- }
-
- public override string GetRemoteName() {
- return _HttpServletRequest.getRemoteHost();
- }
-
-
- public override int GetRemotePort() {
- try {
- return _HttpServletRequest.getRemotePort();
- }
- catch(Exception e) {
- // if servlet API is 2.3 and below - there is no method getRemotePort
- // in ServletRequest interface... should be described as limitation.
- return 0;
- }
- }
-
public override string GetServerVariable(string name) {
// FIXME: We need to make a proper mapping between the standard server
// variables and java equivalent. probably we have to have a configuration file
string headerName = _srvVarsToHeaderMap[name];
if (headerName != null)
- return _HttpServletRequest.getHeader( headerName );
+ return getHeader( headerName );
object knownVariable = KnownServerVariableMap[name];
if (knownVariable != null)
return GetKnownServerVariable((KnownServerVariable)knownVariable);
- return _HttpServletRequest.getHeader( name );
+ return getHeader( name );
}
+ public abstract string GetAuthType ();
+ protected abstract int getContentLength ();
+ protected abstract string getContentType ();
+ public abstract string GetRemoteUser ();
+ protected abstract string getHeader (string name);
+ protected abstract java.util.Enumeration getHeaderNames ();
+ protected abstract InputStream getInputStream ();
+ public abstract ServletContext GetContext ();
+ protected abstract OutputStreamWrapper CreateOutputStream (bool binary);
+
+ public abstract HttpSession GetSession (bool create);
+ public abstract bool IsRequestedSessionIdValid ();
+ public abstract string GetRequestedSessionId ();
+ public abstract bool IsUserInRole (string name);
+ public abstract Principal GetUserPrincipal ();
+
string GetKnownServerVariable(KnownServerVariable index) {
switch (index) {
- case KnownServerVariable.AUTH_TYPE : return _HttpServletRequest.getAuthType();
- case KnownServerVariable.CONTENT_LENGTH : return Convert.ToString(_HttpServletRequest.getContentLength());
- case KnownServerVariable.CONTENT_TYPE : return _HttpServletRequest.getContentType();
+ case KnownServerVariable.AUTH_TYPE : return GetAuthType();
+ case KnownServerVariable.CONTENT_LENGTH : return Convert.ToString(getContentLength());
+ case KnownServerVariable.CONTENT_TYPE : return getContentType();
case KnownServerVariable.QUERY_STRING : return GetQueryString();
case KnownServerVariable.REMOTE_ADDR : return GetRemoteAddress();
case KnownServerVariable.REMOTE_HOST : return GetRemoteName();
- case KnownServerVariable.REMOTE_USER : return _HttpServletRequest.getRemoteUser();
+ case KnownServerVariable.REMOTE_USER : return GetRemoteUser();
case KnownServerVariable.REQUEST_METHOD : return GetHttpVerbName ();
case KnownServerVariable.REQUEST_URI : return GetUriPath();
case KnownServerVariable.SCRIPT_NAME : return GetFilePath ();
case KnownServerVariable.SERVER_NAME : return GetServerName();
- case KnownServerVariable.SERVER_PORT : return Convert.ToString(_HttpServletRequest.getServerPort());
+ case KnownServerVariable.SERVER_PORT : return Convert.ToString(GetLocalPort());
case KnownServerVariable.SERVER_PROTOCOL : return GetHttpVersion ();
- case KnownServerVariable.SERVER_SOFTWARE : return Servlet.getServletContext().getServerInfo();
+ case KnownServerVariable.SERVER_SOFTWARE : return GetContext().getServerInfo();
case KnownServerVariable.PATH_INFO : return GetPathInfo();
default: throw new IndexOutOfRangeException("index");
}
return IntPtr.Zero;
}
- public override string MapPath (string path) {
- string appVirtualPath = GetAppPath ();
- if (path.StartsWith (appVirtualPath))
- path = path.Remove (0, appVirtualPath.Length);
+ public override string GetAppPathTranslated () {
+ return J2EEUtils.GetApplicationRealPath (GetContext ());
+ }
+
+ public override string MapPath (string virtualPath) {
+ if (virtualPath == null)
+ throw new ArgumentNullException ("virtualPath");
- if (path.StartsWith ("/"))
- path = path.Remove (0, 1);
+ ServletContext context = GetContext ();
- string retVal = J2EEUtils.GetApplicationRealPath (_HttpServlet.getServletConfig ());
- Path.Combine (retVal, path);
- if (!retVal.EndsWith ("/") && !retVal.EndsWith ("\\"))
- retVal += "/";
+ string contextPath = GetAppPath ();
+ if ((virtualPath.Length > contextPath.Length && virtualPath [contextPath.Length] != '/') ||
+ string.Compare (contextPath, 0, virtualPath, 0, contextPath.Length, StringComparison.OrdinalIgnoreCase) != 0) {
+
+ for (int appVirtualPathIndex = 0; appVirtualPathIndex > 0 && virtualPath.Length > appVirtualPathIndex; ) {
+ appVirtualPathIndex = virtualPath.IndexOf ('/', appVirtualPathIndex + 1);
+ string crossContextPath = appVirtualPathIndex > 0 ?
+ virtualPath.Remove (appVirtualPathIndex) : virtualPath;
+ ServletContext other = context.getContext (crossContextPath);
+ if (other != null) {
+ string appVirtualPath = appVirtualPathIndex > 0 ?
+ virtualPath.Substring (appVirtualPathIndex) : String.Empty;
+ return other.getRealPath (appVirtualPath);
+ }
+ }
+
+ throw new HttpException (
+ String.Format ("MapPath: Mapping across applications is not allowed. ApplicationPath is '{0}', VirtualPath is '{1}'.",
+ contextPath, virtualPath));
+ }
+
+ string thisAppVirtualPath = virtualPath.Length > contextPath.Length ? virtualPath.Substring (contextPath.Length) : String.Empty;
+ return J2EEUtils.GetApplicationRealPath (context, thisAppVirtualPath);
- return retVal + path;
}
public override void SendResponseFromFile (IntPtr handle, long offset, long length) {
}
}
- public override void SendResponseFromMemory (byte [] data, int length) {
- IPortletActionResponse resp =_HttpServletResponse as IPortletActionResponse;
- if (_OutputStream == null || resp != null && resp.isRedirected())
- return;
+ //kostat
+ //private OutputStreamWrapper CreateOutputStream (bool binary)
+ //{
+ // IPortletActionResponse resp = _HttpServletResponse as IPortletActionResponse;
+ // if (resp != null)
+ // return null; // no output stream while processAction
- sbyte [] sdata = vmw.common.TypeUtils.ToSByteArray(data);
- _OutputStream.write(sdata, 0 , length);
- }
+ // if (_OutputStream != null)
+ // return _OutputStream;
- public override void SendStatus(int statusCode, string statusDescription) {
- // setStatus(int, string) is deprecated
- _HttpServletResponse.setStatus(statusCode/*, statusDescription*/);
- }
+ // if (_HttpServletResponse != null) {
+ // if (binary)
+ // _OutputStream = new OutputStreamWrapper (_HttpServletResponse.getOutputStream ());
+ // else
+ // _OutputStream = new OutputStreamWrapper (_HttpServletResponse.getWriter ());
+ // }
- public override void SendUnknownResponseHeader(string name, string value) {
- if (HeadersSent ())
- return;
+ // return _OutputStream;
+ //}
- _HttpServletResponse.addHeader(name, value);
- }
+ public override void SendResponseFromMemory (byte [] data, int length) {
+ _OutputStream = CreateOutputStream (true);
- public override bool HeadersSent () {
- return _HttpServletResponse.isCommitted();
- }
+ if (_OutputStream == null)
+ return;
- public override void SendCalculatedContentLength (int contentLength) {
- _HttpServletResponse.setContentLength(contentLength);
+ sbyte [] sdata = vmw.common.TypeUtils.ToSByteArray(data);
+ _OutputStream.write(sdata, 0 , length);
}
public override void SendKnownResponseHeader (int index, string value) {
}
public override string GetUnknownRequestHeader (string name) {
- return _HttpServletRequest.getHeader(name);
+ return getHeader(name);
}
public override string [][] GetUnknownRequestHeaders () {
if (unknownHeaders == null) {
ArrayList pairs = new ArrayList ();
- for (java.util.Enumeration he = _HttpServletRequest.getHeaderNames(); he.hasMoreElements() ;) {
+ for (java.util.Enumeration he = getHeaderNames(); he.hasMoreElements() ;) {
string key = (string) he.nextElement();
int index = HttpWorkerRequest.GetKnownRequestHeaderIndex (key);
if (index != -1)
continue;
- pairs.Add (new string [] {key, _HttpServletRequest.getHeader(key)});
+ pairs.Add (new string [] {key, getHeader(key)});
}
if (pairs.Count != 0) {
if (buffer == null || size == 0)
return 0;
sbyte [] sbuffer = vmw.common.TypeUtils.ToSByteArray(buffer);
- InputStream inp = _HttpServletRequest.getInputStream();
- if (inp == null && _HttpServletRequest is IPortletActionRequest)
- inp = ((IPortletActionRequest) _HttpServletRequest).getPortletInputStream();
- int r = (inp != null) ? inp.read(sbuffer, 0, size) : -1;
- return r == -1 ? 0 : r;
+ InputStream inp = getInputStream();
+ if (inp == null)
+ return 0;
+ int r = inp.read (sbuffer, 0, size);
+ return r < 0 ? 0 : r;
}
public override void SetEndOfSendNotification(System.Web.HttpWorkerRequest.EndOfSendNotification callback, object extraData) {
_endOfSendArgs = extraData;
}
- public override string GetProtocol() {
- return _HttpServletRequest.getScheme();
- }
-
- public override string GetServerName() {
- return _HttpServletRequest.getServerName();
- }
-
- public override bool IsSecure() {
- return _HttpServletRequest.isSecure();
- }
+ public abstract BaseHttpContext CreateContext (HttpContext context);
}
}