private static void UpdateResponseHeaders(Request request, Response response, CorsConfiguration corsConfiguration) { if (!request.Headers.Keys.Contains("Origin")) return; response.WithHeader("Access-Control-Allow-Origin", corsConfiguration.AllowOrigin); if (request.Method.Equals("OPTIONS")) { response .WithHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"); } }
private Response AddObject(string bucket, string key, Stream stream) { if (Request.Url.Query == "?acl") { return new Response { StatusCode = HttpStatusCode.OK }; } var content = stream.Copy(configuration.MaxBytesPerSecond); var s3Object = new S3Object { Bucket = bucket, Key = key, ContentType = Request.Headers.ContentType, CreationDate = DateTime.UtcNow, Content = () => content, ContentMD5 = content.GenerateMD5CheckSum(), Size = content.Length }; storage.AddObject(s3Object); var response = new Response { StatusCode = HttpStatusCode.OK }; response.WithHeader("ETag", string.Format("\"{0}\"", s3Object.ContentMD5)); return response; }
private void RegisterCorsOptions(string path) { Options(path, (req) => { var response = this.Context.Response; var res = new Nancy.Response(); res.WithHeader("Access-Control-Allow-Headers", "*"); res.WithHeader("Access-Control-Allow-Origin", "http://localhost:8080"); this.Context.Response = res; return(res); }); }
public HelloModule() { Get["/"] = parameters => "Hello World"; Get["/text/stop_code={stop_code}"] = parameters => "Your stop code is " + parameters.stop_code; Get["/stop_code={stop_code}"] = parameters => { BusStopCreator busStopCreator = new BusStopCreator(); //Create busstop BusStop busStop = busStopCreator.CreateBusStop(parameters.stop_code.ToString()); string str = JsonConvert.SerializeObject(busStop.BusStopInstance); var jsonBytes = Encoding.UTF8.GetBytes(str); return new Response { ContentType = "application/json", Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length) }; }; Get["/bus_stops"] = _ => { List<SimpleBusStop> busStopList = new List<SimpleBusStop>(); StopList stopList = new StopList(); var allstops = stopList.AllStops; foreach (var stop in allstops) { var busstop = new SimpleBusStop(stop.stop_code, stop.stop_id, stop.stop_lat.ToString(), stop.stop_lon.ToString()); busStopList.Add(busstop); } string str = JsonConvert.SerializeObject(busStopList); var jsonBytes = Encoding.UTF8.GetBytes(str); var resp = new Response(); resp.ContentType = "application/json"; resp.Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length); resp.WithHeader("Access-Control-Allow-Origin", "*"); return resp; }; }
/// <summary> /// Force the response to be downloaded as an attachment /// </summary> /// <param name="response">Response object</param> /// <param name="fileName">Filename for the download</param> /// <param name="contentType">Optional content type</param> /// <returns>Mopdified Response object</returns> public static Response AsAttachment(this Response response, string fileName = null, string contentType = null) { var actualFilename = fileName; if (actualFilename == null && response is GenericFileResponse) { actualFilename = ((GenericFileResponse)response).Filename; } if (string.IsNullOrEmpty(actualFilename)) { throw new ArgumentException("fileName cannot be null or empty"); } if (contentType != null) { response.ContentType = contentType; } return(response.WithHeader("Content-Disposition", "attachment; filename=" + actualFilename)); }
private Response GetObject(string bucket, string key) { var s3Object = storage.GetObject(bucket, key); if (s3Object == null) { return new Response { StatusCode = HttpStatusCode.NotFound }; } var stream = s3Object.Content(); var response = new Response { StatusCode = HttpStatusCode.OK, ContentType = s3Object.ContentType }; response.WithHeader("ETag", string.Format("\"{0}\"", s3Object.ContentMD5)); response.WithHeader("Accept-Ranges", "bytes"); response.Contents = x => { var throttledStream = new ThrottledStream(stream, configuration.MaxBytesPerSecond); throttledStream.CopyTo(x); }; return response; }