public virtual RewriterResults rewrite(sRequest request, sResponse original, MutableContent content)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream((content.getContent().Length * 110) / 100);
            OutputStreamWriter output = new OutputStreamWriter(baos);
            String mimeType = original.getHeader("Content-Type");
            if (request.RewriteMimeType != null)
            {
                mimeType = request.RewriteMimeType;
            }
            GadgetSpec spec = null;
            if (request.Gadget != null)
            {
                spec = _specFactory.getGadgetSpec(request.Gadget.toJavaUri(), false);
            }
            if (rewrite(spec, request.getUri(),
                        content,
                        mimeType,
                        output))
            {
                content.setContent(Encoding.Default.GetString(baos.toByteArray()));
                return RewriterResults.cacheableIndefinitely();

            }

            return null;
        }
 /**
  * Utility function to verify that an entry is cacheable and not expired
  * @return true If the response can be used.
  */
 protected bool responseStillUsable(sResponse response)
 {
     if (response == null)
     {
         return(false);
     }
     return(response.getCacheExpiration() > DateTime.UtcNow.Ticks);
 }
Beispiel #3
0
 private static void addHeaders(JsonObject headers, sResponse response, String headerName)
 {
     string[] values = response.getHeaders(headerName);
     if (values != null)
     {
         headers.Put(headerName.ToLower(), new JsonArray(values));
     }
 }
 public HttpResponseBuilder(sResponse response)
 {
     httpStatusCode = response.getHttpStatusCode();
     headers.Add(response.getHeaders());
     foreach (var item in response.getMetadata())
     {
         metadata.Add(item.Key, item.Value);
     }
     responseBytes = response.responseBytes;
 }
Beispiel #5
0
 public HttpResponseBuilder(sResponse response)
 {
     httpStatusCode = response.getHttpStatusCode();
     headers.Add(response.getHeaders());
     foreach (var item in response.getMetadata())
     {
         metadata.Add(item.Key, item.Value);
     }
     responseBytes = response.responseBytes;
 }
        public sResponse removeResponse(sRequest request)
        {
            String    keyString = createKey(request);
            sResponse response  = getResponseImpl(keyString);

            removeResponseImpl(keyString);
            if (responseStillUsable(response))
            {
                return(response);
            }
            return(null);
        }
 public sResponse getResponse(sRequest request)
 {
     if (isCacheable(request))
     {
         String    keyString = createKey(request);
         sResponse cached    = getResponseImpl(keyString);
         if (responseStillUsable(cached))
         {
             return(cached);
         }
     }
     return(null);
 }
Beispiel #8
0
  /**
 * Convert a response to a JSON object.  static so it can be used by HttpPreloaders as well.
 * 
 * The returned JSON object contains the following values:
 * rc: integer response code
 * body: string response body
 * headers: object, keys are header names, values are lists of header values
 * 
 * @param response the response body
 * @param body string to use as the body of the response.
 * @return a JSONObject representation of the response body.
 */
  public static JsonObject getResponseAsJson(sResponse response, String body)
  {
      JsonObject resp = new JsonObject();
      resp.Put("rc", response.getHttpStatusCode());
      resp.Put("body", body);
      JsonObject headers = new JsonObject();
      addHeaders(headers, response, "set-cookie");
      addHeaders(headers, response, "location");
      resp.Put("headers", headers);
      // Merge in additional response data
      foreach (var entry in response.getMetadata())
      {
          resp.Put(entry.Key, entry.Value);
      }
      return resp;
  }
Beispiel #9
0
 public sResponse fetch(sRequest request)
 {
     try
     {
         WebRequest req = request.getRequest();
         req.ContentLength = request.getPostBodyLength();
         if (request.getPostBodyLength() > 0)
         {
             req.GetRequestStream().Write(request.getPostBody(), 0, request.getPostBodyLength());
         }
         sResponse resp = makeResponse(req);
         return(resp);
     }
     catch (Exception)
     {
         return(sResponse.error());
     }
 }
Beispiel #10
0
        public sResponse addResponse(sRequest request, sResponse response) 
        {
            if (isCacheable(request) && isCacheable(response))
            {
                // Both are cacheable. Check for forced cache TTL overrides.
                HttpResponseBuilder responseBuilder = new HttpResponseBuilder(response);
                int forcedTtl = request.CacheTtl;
                if (forcedTtl != -1)
                {
                    responseBuilder.setCacheTtl(forcedTtl);
                }

                response = responseBuilder.create();
                String keyString = createKey(request);
                addResponseImpl(keyString, response);
            }

            return response;
        }
        public sResponse addResponse(sRequest request, sResponse response)
        {
            if (isCacheable(request) && isCacheable(response))
            {
                // Both are cacheable. Check for forced cache TTL overrides.
                HttpResponseBuilder responseBuilder = new HttpResponseBuilder(response);
                int forcedTtl = request.CacheTtl;
                if (forcedTtl != -1)
                {
                    responseBuilder.setCacheTtl(forcedTtl);
                }

                response = responseBuilder.create();
                String keyString = createKey(request);
                addResponseImpl(keyString, response);
            }

            return(response);
        }
Beispiel #12
0
 protected static void SetResponseHeaders(HttpRequestWrapper request, HttpResponse response, sResponse results)
 {
     int refreshInterval;
     if (results.isStrictNoCache())
     {
         refreshInterval = 0;
     }
     else if (request.getParameter(REFRESH_PARAM) != null)
     {
         int.TryParse(request.getParameter(REFRESH_PARAM), out refreshInterval);
     }
     else
     {
         refreshInterval = Math.Max(60 * 60, (int)(results.getCacheTtl() / 1000L));
     }
     HttpUtil.SetCachingHeaders(response, refreshInterval);
     // We're skipping the content disposition header for flash due to an issue with Flash player 10
     // This does make some sites a higher value phishing target, but this can be mitigated by
     // additional referer checks.
     if (!results.getHeader("Content-Type").ToLower().Equals("application/x-shockwave-flash"))
     {
         response.AddHeader("Content-Disposition", "attachment;filename=p.txt");
     }
 }
 protected abstract void addResponseImpl(String key, sResponse response);
Beispiel #14
0
 /**
 * Check if a response might be due to an OAuth protocol error.  We don't want to intercept
 * errors for signed fetch, we only care about places where we are dealing with OAuth request
 * and/or access tokens.
 */
 private bool isFullOAuthError(sResponse response)
 {
     // 400, 401 and 403 are likely to be authentication errors.
     if (response.getHttpStatusCode() != 400 && response.getHttpStatusCode() != 401 &&
         response.getHttpStatusCode() != 403)
     {
         return false;
     }
     // If the client forced us to use full OAuth, this might be OAuth related.
     if (realRequest.getOAuthArguments().mustUseToken())
     {
         return true;
     }
     // If we're using an access token, this might be OAuth related.
     if (accessorInfo.getAccessor().accessToken != null)
     {
         return true;
     }
     // Not OAuth related.
     return false;
 }
Beispiel #15
0
        protected override void addResponseImpl(String key, sResponse response)
        {
            HttpRuntime.Cache.Add(key, response, null, Cache.NoAbsoluteExpiration, new TimeSpan(0,5,0), CacheItemPriority.Normal, null);
Beispiel #16
0
 protected override void addResponseImpl(String key, sResponse response)
 {
     HttpRuntime.Cache.Add(key, response, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0), CacheItemPriority.Normal, null);
 }
Beispiel #17
0
 /**
  * Utility function to verify that an entry is cacheable and not expired
  * @return true If the response can be used.
  */
 protected bool responseStillUsable(sResponse response)
 {
     if (response == null)
     {
         return false;
     }
     return response.getCacheExpiration() > DateTime.UtcNow.Ticks;
 }
Beispiel #18
0
 /**
 * Add a request/response pair to our trace of actions associated with this request.
 */
 public void addRequestTrace(sRequest request, sResponse response)
 {
     requestTrace.Add(new Pair(request, response));
 }
        public sResponse rewriteHttpResponse(sRequest req, sResponse resp)
        {
            String originalContent = resp.responseString;
            MutableContent mc = GetMutableContent(originalContent);

            foreach(IContentRewriter rewriter in rewriters)
            {
                rewriter.rewrite(req, resp, mc);
            }

            String rewrittenContent = mc.getContent();
            if (rewrittenContent.Equals(originalContent)) 
            {
                return resp;
            }

            return new HttpResponseBuilder(resp).setResponseString(rewrittenContent).create();
        }
 protected bool isCacheable(sResponse response)
 {
     return(!response.isStrictNoCache());
 }
Beispiel #21
0
 protected abstract void addResponseImpl(String key, sResponse response);
Beispiel #22
0
        /**
       * Format a response as JSON, including additional JSON inserted by
       * chained content fetchers.
       */
        private String convertResponseToJson(ISecurityToken authToken, HttpRequestWrapper request, sResponse results)
        {
            try
            {
                String originalUrl = request.getParameter(URL_PARAM);
                String body = results.responseString;
                if ("FEED".Equals(request.getParameter(CONTENT_TYPE_PARAM)))
                {
                    body = processFeed(originalUrl, request, body);
                }
                JsonObject resp = FetchResponseUtils.getResponseAsJson(results, body);

                if (authToken != null)
                {
                    String updatedAuthToken = authToken.getUpdatedToken();
                    if (updatedAuthToken != null)
                    {
                        resp.Put("st", updatedAuthToken);
                    }
                }
                // Use raw param as key as URL may have to be decoded
                return new JsonObject().Put(originalUrl, resp).ToString();
            }
            catch (JsonException)
            {
                return "";
            }
        }
Beispiel #23
0
 protected bool isCacheable(sResponse response)
 {
     return !response.isStrictNoCache();
 }
 public RewriterResults rewrite(sRequest req, sResponse resp,  MutableContent content) 
 {
     return null;
 }
Beispiel #25
0
 public RewriterResults rewrite(sRequest req, sResponse resp, MutableContent content)
 {
     return RewriterResults.cacheableIndefinitely();
 }
Beispiel #26
0
        /**
        * Parse OAuth WWW-Authenticate header and either add them to an existing
        * message or create a new message.
        *
        * @param msg
        * @param resp
        * @return the updated message.
        */
        private static OAuthMessage parseAuthHeader(OAuthMessage msg, sResponse resp)
        {
            if (msg == null)
            {
                msg = new OAuthMessage(null, null, null);
            }

            foreach (String auth in resp.getHeaders("WWW-Authenticate"))
            {
                msg.addParameters(OAuthMessage.decodeAuthorization(auth));
            }

            return msg;
        }
Beispiel #27
0
 /**
 * Look for an OAuth protocol problem.  For cases where no access token is in play 
 * @param response
 * @throws OAuthProtocolException
 * @throws IOException
 */
 private void checkForProtocolProblem(sResponse response)
 {
     if (isFullOAuthError(response))
     {
         OAuthMessage message = parseAuthHeader(null, response);
         if (message.getParameter(OAuthProblemException.OAUTH_PROBLEM) != null)
         {
             // SP reported extended error information
             throw new OAuthProtocolException(message);
         }
         // No extended information, guess based on HTTP response code.
         throw new OAuthProtocolException(response.getHttpStatusCode());
     }
 }
Beispiel #28
0
 public HttpPreloadData(sResponse response, String key) 
 {
     JsonObject _data;
     try 
     {
         _data = FetchResponseUtils.getResponseAsJson(response, response.responseString);
     } 
     catch (JsonException) 
     {
         _data = new JsonObject();
     }
     this.data = _data;
     this.key = key;
 }