public void NonCaching_search_without_apikey_passes_auth_info_to_lucene() { var search = _deki.AtLocalHost.At("site", "search"); var searchMock = CreateMocks().Item1; var luceneXml = new XDoc("lucene"); var searchQuery = new SearchQuery("raw", "processed", new LuceneClauseBuilder(), null); searchMock.Setup(x => x.BuildQuery("foo", "", SearchQueryParserType.BestGuess, false)).Returns(searchQuery).AtMostOnce().Verifiable(); XUri luceneUriCalled = null; MockPlug.Register(Utils.Settings.LuceneMockUri, (p, v, u, req, res) => { luceneUriCalled = u; res.Return(DreamMessage.Ok(luceneXml)); }); var response = search .With("q", "foo") .With("nocache", true) .Get(new Result <DreamMessage>()).Wait(); Assert.IsTrue(response.IsSuccessful, response.ToErrorString()); Assert.IsNotNull(luceneUriCalled, "lucene was not called"); Assert.AreEqual(Utils.Settings.LuceneMockUri, luceneUriCalled.WithoutQuery(), "lucene was called at wrong uri"); Assert.IsNotNull(luceneUriCalled.GetParam("apiuri"), "lucene request did not contain an apiuri parameter"); Assert.IsNotNull(luceneUriCalled.GetParam("userid"), "lucene request did not contain a userid parameter"); Assert.AreEqual(searchQuery.LuceneQuery, luceneUriCalled.GetParam("q"), "lucene request had incorrect q parameter"); }
/// <summary> /// Get a named parameter. /// </summary> /// <remarks> /// Will throw <see cref="DreamAbortException"/> if the named parameter does not exist. /// </remarks> /// <param name="key"><see cref="DreamFeatureParamAttribute"/> name.</param> /// <returns>Text value of parameter.</returns> /// <exception cref="DreamAbortException">Throws if parameter does not exist.</exception> public string GetParam(string key) { EnsureFeatureIsSet(); if (key == null) { throw new ArgumentNullException("key"); } string result; string[] values; _pathParams.TryGetValue(key, out values); if ((values != null) && (values.Length > 0)) { result = values[0]; } else { result = Uri.GetParam(key, null); } if (result == null) { throw new DreamAbortException(DreamMessage.BadRequest(string.Format("missing feature parameter '{0}'", key))); } return(result); }
//--- Class Methods --- internal static AMedia New(XUri uri, XDoc config) { // check if the uri is a viddler video if (uri.Scheme.EqualsInvariantIgnoreCase("kaltura")) { if (uri.Segments.Length >= 1) { string entryID = uri.Segments[0]; string partnerID = config["kaltura/partner-id"].AsText; // check if extension is configured for kaltura integration if (!string.IsNullOrEmpty(partnerID)) { bool remix = !(uri.GetParam("edit", null) ?? uri.GetParam("remix", "no")).EqualsInvariantIgnoreCase("no"); // verify that user has permission to remix content on current page if (remix) { Plug dekiApi = GetDekiApi(config); if (dekiApi != null) { try { DekiScriptMap env = DreamContext.Current.GetState <DekiScriptMap>(); string pageid = env.GetAt("page.id").AsString(); string userid = env.GetAt("user.id").AsString(); XDoc users = dekiApi.At("pages", pageid, "allowed").With("permissions", "UPDATE").Post(new XDoc("users").Start("user").Attr("id", userid).End()).ToDocument(); remix = !users[string.Format(".//user[@id='{0}']", userid)].IsEmpty; } catch (Exception e) { _log.Error("unable to verify user permission on page", e); } } } // check if SEO links are explicitly disabled bool seo = !(config["kaltura/seo-links"].AsText ?? "enabled").EqualsInvariantIgnoreCase("disabled"); // determin which UI configuration to use based on user's permissions and embed settings for video string uiConfID = remix ? config["kaltura/uiconf/player-mix"].AsText : config["kaltura/uiconf/player-nomix"].AsText; if (!string.IsNullOrEmpty(uiConfID)) { uri = config["kaltura/server-uri"].AsUri ?? new XUri("http://www.kaltura.com"); uri = uri.At("index.php", "kwidget", "wid", "_" + partnerID, "uiconf_id", uiConfID, "entry_id", entryID); return(new KalturaVideo(uri, remix, seo)); } } } } return(null); }
public XDoc GadgetScript( [DekiExtParam("Google gadget script")] string script ) { // validate xml XDoc xml = XDocFactory.From("<html><body>" + script + "</body></html>", MimeType.HTML)["//script"]; if ((xml == null) || !xml.HasName("script")) { throw new ArgumentException("Google gadget must contained in a <script> tag"); } // validate uri XUri uri = xml["@src"].AsUri ?? XUri.Localhost; if (!uri.Host.EqualsInvariantIgnoreCase("gmodules.com") && !uri.Host.EqualsInvariantIgnoreCase("www.gmodules.com")) { throw new ArgumentException("Google gadget must be originating from gmodules.com"); } // remove .js output option uri = uri.WithoutParams("output"); // create <iframe> in which we'll load the gadget return(NewIFrame(uri, SysUtil.ChangeType <float?>(uri.GetParam("w")), SysUtil.ChangeType <float>(uri.GetParam("h")))); }
/// <summary> /// Get a typed parameter from the Uri. /// </summary> /// <typeparam name="T">Type of the parameter.</typeparam> /// <param name="uri">Input Uri.</param> /// <param name="key">Parameter key.</param> /// <param name="index">Parameter index.</param> /// <param name="def">Default value to return in case parameter does not exist.</param> /// <returns>Parameter value or default.</returns> public static T GetParam <T>(this XUri uri, string key, int index, T def) { string value = uri.GetParam(key, index, null); if (!string.IsNullOrEmpty(value)) { return((T)SysUtil.ChangeType(value, typeof(T))); } return(def); }
public void TestQueryEncoding() { XUri uri = new XUri("http://foo/bar"); uri = uri.With("x", "a=b"); Assert.AreEqual("a=b", uri.GetParam("x")); XUri uri2 = new XUri(uri.ToString()); Assert.AreEqual("a=b", uri2.GetParam("x")); }
public Yield Invoke(Plug plug, string verb, XUri uri, DreamMessage request, Result <DreamMessage> response) { // we only support GET as verb DreamMessage reply; if ((verb != Verb.GET) && (verb != Verb.HEAD)) { reply = new DreamMessage(DreamStatus.MethodNotAllowed, null, null); reply.Headers.Allow = Verb.GET + "," + Verb.HEAD; } else { bool head = (verb == Verb.HEAD); // try to load the assembly System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(uri.Host); Version version = assembly.GetName().Version; DateTime timestamp = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2); // check if request is just about re-validation if (!head && request.CheckCacheRevalidation(timestamp)) { reply = DreamMessage.NotModified(); } else { try { System.IO.Stream stream = assembly.GetManifestResourceStream(uri.Path.Substring(1)); if (stream != null) { MimeType mime = MimeType.New(uri.GetParam(DreamOutParam.TYPE, null)) ?? MimeType.BINARY; reply = new DreamMessage(DreamStatus.Ok, null, mime, stream.Length, head ? System.IO.Stream.Null : stream); if (head) { stream.Close(); } else { reply.SetCacheMustRevalidate(timestamp); } } else { reply = DreamMessage.NotFound("could not find resource"); } } catch (System.IO.FileNotFoundException) { reply = DreamMessage.NotFound("could not find resource"); } catch (Exception e) { reply = DreamMessage.InternalError(e); } } } response.Return(reply); yield break; }
//--- Class Methods --- internal static AMedia New(XUri uri) { // check if the uri is a google video if (uri.Host.EqualsInvariantIgnoreCase("video.google.com")) { if ((null != uri.Params) && (!String.IsNullOrEmpty(uri.GetParam("docid")))) { return(new GoogleVideo(new XUri("http://video.google.com/googleplayer.swf?docId=" + uri.GetParam("docid")))); } } return(null); }
private XDoc CreateExportDocumentFromList(string listPath) { if (!File.Exists(listPath)) { throw new ConfigurationException("No such export list: {0}", listPath); } XDoc exportDoc = new XDoc("export"); foreach (string line in File.ReadAllLines(listPath)) { if (string.IsNullOrEmpty(line)) { continue; } if (line.StartsWith("#")) { exportDoc.Comment(line.Remove(0, 1)); continue; } try { bool exportRecursive = _exportRecursive; string path = line.Trim(); if (path.EndsWith(" +")) { exportRecursive = true; path = path.Substring(0, path.Length - 2).TrimEnd(); } else if (path.EndsWith(" -")) { exportRecursive = false; path = path.Substring(0, path.Length - 2).TrimEnd(); } if (!line.StartsWith("/")) { XUri uri = new XUri(path); path = uri.Path; if ("/index.php".EqualsInvariantIgnoreCase(path)) { path = uri.GetParam("title"); } } exportDoc.Start("page") .Attr("path", Title.FromUIUri(null, path).AsPrefixedDbPath()) .Attr("recursive", exportRecursive) .End(); } catch (Exception) { throw new ConfigurationException("Unable to parse uri: {0}", line.Trim()); } } return(exportDoc); }
public void Caching_search_with_apikey_does_not_pass_auth_info_to_lucene() { var searchMock = CreateMocks().Item1; var responseXml = new XDoc("response"); var luceneXml = new XDoc("lucene"); var searchQuery = new SearchQuery("raw", "processed", new LuceneClauseBuilder(), null); var searchResult = new SearchResult(); searchMock.Setup(x => x.BuildQuery("foo", "", SearchQueryParserType.BestGuess, true)).Returns(searchQuery).AtMostOnce().Verifiable(); searchMock.Setup(x => x.GetCachedQuery(searchQuery)).Returns((SearchResult)null).AtMostOnce().Verifiable(); XUri luceneUriCalled = null; MockPlug.Register(Utils.Settings.LuceneMockUri, (p, v, u, req, res) => { luceneUriCalled = u; res.Return(DreamMessage.Ok(luceneXml)); }); searchMock.Setup(x => x.CacheQuery(It.Is <XDoc>(v => v == luceneXml), It.IsAny <SearchQuery>(), It.IsAny <TrackingInfo>())) .Returns(searchResult); searchMock.Setup(x => x.FormatResultSet( searchResult, It.IsAny <SetDiscriminator>(), false, It.IsAny <TrackingInfo>(), It.IsAny <Result <XDoc> >() )) .Returns(new Result <XDoc>().WithReturn(responseXml)); var response = _search .With("q", "foo") .With("apikey", Utils.Settings.ApiKey) .Get(new Result <DreamMessage>()).Wait(); Assert.IsTrue(response.IsSuccessful, response.ToErrorString()); Assert.IsNotNull(luceneUriCalled, "lucene was not called"); Assert.AreEqual(Utils.Settings.LuceneMockUri.At("compact"), luceneUriCalled.WithoutQuery(), "lucene was called at wrong uri"); Assert.IsNull(luceneUriCalled.GetParam("apiuri"), "lucene request contained an apiuri parameter"); Assert.IsNull(luceneUriCalled.GetParam("userid"), "lucene request contained a userid parameter"); Assert.AreEqual(searchQuery.LuceneQuery, luceneUriCalled.GetParam("q"), "lucene request had incorrect q parameter"); }
public void DreamIn_Origin_Host_and_Uri_are_not_stripped_if_the_host_doesn_not_require_DreamIn_Auth() { using (var hostinfo = DreamTestHelper.CreateRandomPortHost()) { var recipient = hostinfo.CreateMockService(); XUri incomingUri = null; recipient.Service.CatchAllCallback = (context, request, response) => { incomingUri = context.Uri; response.Return(DreamMessage.Ok()); }; var recipientUri = recipient.AtLocalHost.Uri.WithScheme("ext-http"); Plug.New(recipientUri) .With(DreamInParam.ROOT, "root") .With(DreamInParam.ORIGIN, "origin") .With(DreamInParam.HOST, "host") .With(DreamInParam.URI, "http://uri") .Get(); Assert.IsNotNull(incomingUri); Assert.AreEqual("root", incomingUri.GetParam(DreamInParam.ROOT)); Assert.AreEqual("origin", incomingUri.GetParam(DreamInParam.ORIGIN)); Assert.AreEqual("host", incomingUri.GetParam(DreamInParam.HOST)); Assert.AreEqual("http://uri", incomingUri.GetParam(DreamInParam.URI)); } }
public void DreamIn_Origin_Host_and_Uri_are_stripped_if_DreamInAuth_header_is_not_set_on_host_requiring_DreamIn_Auth() { var recipient = _hostinfo.CreateMockService(); XUri incomingUri = null; recipient.Service.CatchAllCallback = (context, request, response) => { incomingUri = context.Uri; response.Return(DreamMessage.Ok()); }; var recipientUri = recipient.AtLocalHost.Uri.WithScheme("ext-http"); Plug.New(recipientUri) .With(DreamInParam.ROOT, "root") .With(DreamInParam.ORIGIN, "origin") .With(DreamInParam.HOST, "host") .With(DreamInParam.URI, "http://uri") .Get(); Assert.IsNotNull(incomingUri); Assert.IsNull(incomingUri.GetParam(DreamInParam.ROOT)); Assert.IsNull(incomingUri.GetParam(DreamInParam.ORIGIN)); Assert.IsNull(incomingUri.GetParam(DreamInParam.HOST)); Assert.IsNull(incomingUri.GetParam(DreamInParam.URI)); }
public static string ConvertPageUriToPath(string uri) { //Converts page paths from MT Page xml (uri.ui) to paths that can be accepted by dekiscript XUri xuri = null; string ret = null; if (XUri.TryParse(uri, out xuri)) { if (xuri.Path.ToLowerInvariant() == "/index.php") { ret = xuri.GetParam("title", uri); } else { ret = xuri.Path; } } return(ret); }
public void TestUriConstructor24() { string original = "http://host/seg^ment?qu^ery=a|b^c#fo|o#b^ar"; XUri uri = new XUri(original); Assert.AreEqual("http", uri.Scheme); Assert.AreEqual("host", uri.Host); Assert.AreEqual(80, uri.Port); Assert.AreEqual(true, uri.UsesDefaultPort); Assert.AreEqual(null, uri.User); Assert.AreEqual(null, uri.Password); Assert.AreEqual("/seg^ment", uri.Path); Assert.AreEqual(1, uri.Segments.Length); Assert.AreEqual(false, uri.TrailingSlash); Assert.AreEqual("qu^ery=a|b^c", uri.Query); Assert.AreEqual("a|b^c", uri.GetParam("qu^ery")); Assert.AreEqual("fo|o#b^ar", uri.Fragment); Assert.AreEqual(original, uri.ToString()); }
//--- Class Methods --- internal static AMedia New(XUri uri) { // check if the uri is a youtube video if ( uri.Host.EndsWithInvariantIgnoreCase(".youtube.com") || uri.Host.EqualsInvariantIgnoreCase("youtube.com") || uri.Host.EndsWithInvariantIgnoreCase(".youtube-nocookie.com") || uri.Host.EqualsInvariantIgnoreCase("youtube-nocookie.com") ) { if (uri.GetParam("v") != null) { return(new YouTubeVideo(uri.WithoutPathQueryFragment().At("v", uri.GetParam("v")))); } if (!ArrayUtil.IsNullOrEmpty(uri.Segments) && (uri.Segments.Length == 2) && (uri.Segments[0].EqualsInvariantIgnoreCase("v"))) { return(new YouTubeVideo(uri)); } } return(null); }
public void TestUriConstructor24() { string original = "http://host/seg^ment?qu^ery=a|b^c#fo|o#b^ar"; XUri uri = new XUri(original); Assert.AreEqual("http", uri.Scheme); Assert.AreEqual("host", uri.Host); Assert.AreEqual(80, uri.Port); Assert.AreEqual(true, uri.UsesDefaultPort); Assert.AreEqual(null, uri.User); Assert.AreEqual(null, uri.Password); Assert.AreEqual("/seg^ment", uri.Path); Assert.AreEqual(1, uri.Segments.Length); Assert.AreEqual(false, uri.TrailingSlash); Assert.AreEqual("qu^ery=a|b^c", uri.Query); Assert.AreEqual("a|b^c", uri.GetParam("qu^ery")); Assert.AreEqual("fo|o#b^ar", uri.Fragment); Assert.AreEqual(original, uri.ToString()); }
//--- Methods --- private int GetMatchScore(string verb, XUri uri, DreamMessage request) { var score = 0; if (verb.EqualsInvariantIgnoreCase(_verb)) { score = 1; } else if (_verb != "*") { return(0); } var path = _matchTrailingSlashes ? _uri.Path : _uri.WithoutTrailingSlash().Path; var incomingPath = _matchTrailingSlashes ? uri.Path : uri.WithoutTrailingSlash().Path; if (!incomingPath.EqualsInvariantIgnoreCase(path)) { return(0); } score++; if (_uri.Params != null) { foreach (var param in _uri.Params) { var v = uri.GetParam(param.Key); if (v == null || !v.EndsWithInvariantIgnoreCase(param.Value)) { return(0); } score++; } } foreach (var matcher in _queryMatchers) { var v = uri.GetParam(matcher.Item1); if (v == null || !matcher.Item2(v)) { return(0); } score++; } foreach (var matcher in _headerMatchers) { var v = request.Headers[matcher.Item1]; if (string.IsNullOrEmpty(v) || !matcher.Item2(v)) { return(0); } score++; } foreach (var header in _headers) { var v = request.Headers[header.Key]; if (string.IsNullOrEmpty(v) || !v.EqualsInvariant(header.Value)) { return(0); } score++; } if (_requestCallback != null) { if (!_requestCallback(request)) { return(0); } } else if (_request != null && (!request.HasDocument || _request != request.ToDocument())) { return(0); } score++; return(score); }
private void RequestHandler(IAsyncResult ar) { HttpListenerContext httpContext = null; Action <string> activity = null; HttpListener listener = (HttpListener)ar.AsyncState; // try to finish getting the current context try { httpContext = listener.EndGetContext(ar); } catch (Exception e) { _log.WarnExceptionFormat(e, "unable to finish acquiring the request context, unable to handle request"); } // start listening for next request if (!listener.IsListening) { _log.Debug("dropping out of request handler, since the listener is no longer listening"); return; } try { listener.BeginGetContext(RequestHandler, listener); } catch (Exception e) { _log.WarnExceptionFormat(e, "unable to re-aquire context, dropping out of request handler"); return; } // if we didn't succeed in ending the GetContext call, drop out if (httpContext == null) { return; } DreamMessage request = null; try { // finish listening for current context string[] prefixes = new string[listener.Prefixes.Count]; listener.Prefixes.CopyTo(prefixes, 0); XUri requestUri = HttpUtil.FromHttpContext(httpContext); _log.DebugMethodCall("RequestHandler", httpContext.Request.HttpMethod, requestUri); // create request message request = new DreamMessage(DreamStatus.Ok, new DreamHeaders(httpContext.Request.Headers), MimeType.New(httpContext.Request.ContentType), httpContext.Request.ContentLength64, httpContext.Request.InputStream); DreamUtil.PrepareIncomingMessage(request, httpContext.Request.ContentEncoding, prefixes[0], httpContext.Request.RemoteEndPoint.ToString(), httpContext.Request.UserAgent); requestUri = requestUri.AuthorizeDreamInParams(request, _dreamInParamAuthtoken); // check if the request was forwarded through Apache mod_proxy string hostname = requestUri.GetParam(DreamInParam.HOST, null) ?? request.Headers.ForwardedHost ?? request.Headers.Host ?? requestUri.HostPort; activity = new ActivityState(_env, httpContext.Request.HttpMethod, httpContext.Request.Url.ToString(), hostname).Message; activity("RequestHandler"); // process message _env.UpdateInfoMessage(_sourceExternal, null); string verb = httpContext.Request.HttpMethod; _env.SubmitRequestAsync(verb, requestUri, httpContext.User, request, new Result <DreamMessage>(TimeSpan.MaxValue)) .WhenDone(result => Coroutine.Invoke(ResponseHandler, request, result, httpContext, activity, new Result(TimeSpan.MaxValue))); } catch (Exception ex) { _log.ErrorExceptionMethodCall(ex, "RequestHandler"); if (request != null) { request.Close(); } try { DreamMessage response = DreamMessage.InternalError(ex); httpContext.Response.StatusCode = (int)response.Status; Stream stream = response.ToStream(); httpContext.Response.Headers.Clear(); foreach (KeyValuePair <string, string> pair in response.Headers) { HttpUtil.AddHeader(httpContext.Response, pair.Key, pair.Value); } httpContext.Response.KeepAlive = false; long size = response.ContentLength; if (((size == -1) || (size > 0)) && (stream != Stream.Null)) { CopyStream(delegate { }, stream, httpContext.Response.OutputStream, size, new Result <long>(DreamHostService.MAX_REQUEST_TIME)).Block(); } httpContext.Response.OutputStream.Flush(); } catch { httpContext.Response.StatusCode = (int)DreamStatus.InternalError; } httpContext.Response.Close(); if (activity != null) { activity(null); } } }
public void TestQueryEncoding() { XUri uri = new XUri("http://foo/bar"); uri = uri.With("x", "a=b"); Assert.AreEqual("a=b", uri.GetParam("x")); XUri uri2 = new XUri(uri.ToString()); Assert.AreEqual("a=b", uri2.GetParam("x")); }
//--- Methods --- private int GetMatchScore(string verb, XUri uri, DreamMessage request) { var score = 0; if(verb.EqualsInvariantIgnoreCase(_verb)) { score = 1; } else if(_verb != "*") { return 0; } var path = _matchTrailingSlashes ? _uri.Path : _uri.WithoutTrailingSlash().Path; var incomingPath = _matchTrailingSlashes ? uri.Path : uri.WithoutTrailingSlash().Path; if(!incomingPath.EqualsInvariantIgnoreCase(path)) { return 0; } score++; if(_uri.Params != null) { foreach(var param in _uri.Params) { var v = uri.GetParam(param.Key); if(v == null || !v.EndsWithInvariantIgnoreCase(param.Value)) { return 0; } score++; } } foreach(var matcher in _queryMatchers) { var v = uri.GetParam(matcher.Item1); if(v == null || !matcher.Item2(v)) { return 0; } score++; } foreach(var matcher in _headerMatchers) { var v = request.Headers[matcher.Item1]; if(string.IsNullOrEmpty(v) || !matcher.Item2(v)) { return 0; } score++; } foreach(var header in _headers) { var v = request.Headers[header.Key]; if(string.IsNullOrEmpty(v) || !v.EqualsInvariant(header.Value)) { return 0; } score++; } if(_requestCallback != null) { if(!_requestCallback(request)) { return 0; } } else if(_request != null && (!request.HasDocument || _request != request.ToDocument())) { return 0; } score++; return score; }