Exemplo n.º 1
0
        /**
         * Construct an HttpResponse from a builder (called by HttpResponseBuilder.create).
         */
        public sResponse(HttpResponseBuilder builder)
        {
            httpStatusCode = builder.getHttpStatusCode();
            NameValueCollection headerCopy = new NameValueCollection {
                builder.getHeaders()
            };

            // Always safe, HttpResponseBuilder won't modify the body.
            responseBytes = builder.getResponse();

            Dictionary <String, String> metadataCopy = new Dictionary <string, string>(builder.getMetadata());

            metadata = metadataCopy;

            // We want to modify the headers to ensure that the proper Content-Type and Date headers
            // have been set. This allows us to avoid these expensive calculations from the cache.
            date     = getAndUpdateDate(headerCopy);
            headers  = headerCopy;
            encoding = getAndUpdateEncoding(headerCopy, responseBytes);
            Encoding encoder = Encoding.GetEncoding(encoding);

            if (responseBytes != null)
            {
                responseString = encoder.GetString(responseBytes);
            }

            // Strip BOM if present
            if (responseString.Length > 0 && responseString[0] == 0xFEFF)
            {
                responseString = responseString.Substring(1);
            }
        }
Exemplo n.º 2
0
        public HttpResponseBuilder(HttpResponseBuilder builder)
        {
            httpStatusCode = builder.httpStatusCode;
            headers.Add(builder.headers);
            foreach (var item in builder.metadata)
            {
                metadata.Add(item.Key, item.Value);
            }

            responseBytes = builder.responseBytes;
        }
Exemplo n.º 3
0
        public HttpResponseBuilder(HttpResponseBuilder builder)
        {
            httpStatusCode = builder.httpStatusCode;
            headers.Add(builder.headers);
            foreach (var item in builder.metadata)
            {
                metadata.Add(item.Key, item.Value);
            }

            responseBytes = builder.responseBytes;
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /**
       * Construct an HttpResponse from a builder (called by HttpResponseBuilder.create).
       */
        public sResponse(HttpResponseBuilder builder)
        {
            httpStatusCode = builder.getHttpStatusCode();
            NameValueCollection headerCopy = new NameValueCollection {builder.getHeaders()};

            // Always safe, HttpResponseBuilder won't modify the body.
            responseBytes = builder.getResponse();

            Dictionary<String, String> metadataCopy = new Dictionary<string,string>(builder.getMetadata());
            metadata = metadataCopy;

            // We want to modify the headers to ensure that the proper Content-Type and Date headers
            // have been set. This allows us to avoid these expensive calculations from the cache.
            date = getAndUpdateDate(headerCopy);
            headers = headerCopy;
            encoding = getAndUpdateEncoding(headerCopy, responseBytes);
            Encoding encoder = Encoding.GetEncoding(encoding);
            if (responseBytes != null)
            {
                responseString = encoder.GetString(responseBytes);
            }

            // Strip BOM if present
            if (responseString.Length > 0 && responseString[0] == 0xFEFF)
            {
                responseString = responseString.Substring(1);
            }
        }
Exemplo n.º 7
0
 /**
 * Access token data is returned to the gadget as json key/value pairs:
 *
 *    { "user_id": "12345678" }
 */
 private HttpResponseBuilder formatAccessTokenData()
 {
     HttpResponseBuilder builder = new HttpResponseBuilder();
     builder.setHeader("Content-Type", "application/json; charset=utf-8");
     builder.setHttpStatusCode((int)HttpStatusCode.OK);
     // no need to cache this, these requests should be fairly rare, and the results should be
     // cached in gadget.
     builder.setStrictNoCache();
     JsonObject json = new JsonObject(accessTokenData);
     builder.setResponseString(json.ToString());
     return builder;
 }
Exemplo n.º 8
0
 /**
 * Get honest-to-goodness user data.
 *
 * @throws OAuthProtocolException if the service provider returns an OAuth
 * related error instead of user data.
 */
 private HttpResponseBuilder fetchData()
 {
     HttpResponseBuilder builder;
     if (accessTokenData != null)
     {
         // This is a request for access token data, return it.
         builder = formatAccessTokenData();
     }
     else
     {
         sRequest signed = sanitizeAndSign(realRequest, null);
         sResponse response = fetchFromServer(signed);
         checkForProtocolProblem(response);
         builder = new HttpResponseBuilder(response);
     }
     return builder;
 }
Exemplo n.º 9
0
        /**
        * Fetch data and build a response to return to the client.  We try to always return something
        * reasonable to the calling app no matter what kind of madness happens along the way.  If an
        * unchecked exception occurs, well, then the client is out of luck.
        */
        private sResponse fetchNoThrow() 
        {
            HttpResponseBuilder response = null;
            try 
            {
                accessorInfo = fetcherConfig.getTokenStore().getOAuthAccessor(
                realRequest.getSecurityToken(), realRequest.getOAuthArguments(), clientState,
                responseParams);
                response = fetchWithRetry();
            } 
            catch (OAuthResponseParams.OAuthRequestException e) 
            {
                // No data for us.
                responseParams.logDetailedWarning("OAuth fetch fatal error", e);
                responseParams.setSendTraceToClient(true);
                if (response == null) 
                {
                    response = new HttpResponseBuilder()
                    .setHttpStatusCode(sResponse.SC_FORBIDDEN);
                    responseParams.addToResponse(response);
                    return response.create();
                }
            }

            // OK, got some data back, annotate it as necessary.
            if (response.getHttpStatusCode() >= 400) 
            {
                responseParams.logDetailedWarning("OAuth fetch fatal error");
                responseParams.setSendTraceToClient(true);
            } 
            else if (responseParams.getAznUrl() != null && responseParams.sawErrorResponse()) 
            {
                responseParams.logDetailedWarning("OAuth fetch error, reprompting for user approval");
                responseParams.setSendTraceToClient(true);
            }

            responseParams.addToResponse(response);

            return response.create();
        }
Exemplo n.º 10
0
 public void addToResponse(HttpResponseBuilder response)
 {
     if (!newClientState.isEmpty())
     {
         try
         {
             response.setMetadata(CLIENT_STATE, newClientState.getEncryptedState());
         }
         catch (BlobCrypterException e)
         {
             // Configuration error somewhere, this should never happen.
             throw e;
         }
     }
     if (!String.IsNullOrEmpty(aznUrl))
     {
         response.setMetadata(APPROVAL_URL, aznUrl);
     }
     if (error != null)
     {
         response.setMetadata(ERROR_CODE, error);
     }
     if (errorText != null || sendTraceToClient)
     {
         StringBuilder verboseError = new StringBuilder();
         if (errorText != null)
         {
             verboseError.Append(errorText);
         }
         if (sendTraceToClient)
         {
             verboseError.Append('\n');
             verboseError.Append(getRequestTrace());
         }
         response.setMetadata(ERROR_TEXT, verboseError.ToString());
     }
 }