ravendb /Raven.Database/Server/Controllers/RavenBaseApiController.cs

Language C# Lines 616
MD5 Hash d032a4690de6f0fedb1ffdad6da644ba
Repository https://github.com/barryhagan/ravendb.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using ICSharpCode.SharpZipLib.Zip;
using Mono.CSharp;
using Raven.Abstractions.Connection;
using Raven.Abstractions.Data;
using Raven.Abstractions.Exceptions;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Json;
using Raven.Abstractions.Logging;
using Raven.Abstractions.Util;
using Raven.Client.Connection;
using Raven.Database.Config;
using Raven.Database.Server.Abstractions;
using Raven.Database.Server.WebApi;
using Raven.Imports.Newtonsoft.Json;
using Raven.Imports.Newtonsoft.Json.Bson;
using Raven.Imports.Newtonsoft.Json.Linq;
using Raven.Json.Linq;
using HttpExtensions = Raven.Database.Extensions.HttpExtensions;

namespace Raven.Database.Server.Controllers
{
	public abstract class RavenBaseApiController : ApiController
	{
		protected static readonly ILog Log = LogManager.GetCurrentClassLogger();

		private HttpRequestMessage request;

		public HttpRequestMessage InnerRequest
		{
			get
			{
				return Request ?? request;
			}
		}

		public HttpHeaders InnerHeaders
		{
			get
			{
				var headers = new Headers();
				foreach (var header in InnerRequest.Headers)
				{
					if (header.Value.Count() == 1)
						headers.Add(header.Key, header.Value.First());
					else
						headers.Add(header.Key, header.Value.ToList());
				}

				if (InnerRequest.Content == null)
					return headers;

				foreach (var header in InnerRequest.Content.Headers)
				{
					if (header.Value.Count() == 1)
						headers.Add(header.Key, header.Value.First());
					else
						headers.Add(header.Key, header.Value.ToList());
				}

				return headers;
			}
		}

		public new IPrincipal User { get; set; }

		protected virtual void InnerInitialization(HttpControllerContext controllerContext)
		{
			request = controllerContext.Request;
			User = controllerContext.RequestContext.Principal;
		}

		public async Task<T> ReadJsonObjectAsync<T>()
		{
			using (var stream = await InnerRequest.Content.ReadAsStreamAsync())
			//using(var gzipStream = new GZipStream(stream, CompressionMode.Decompress))
			using (var streamReader = new StreamReader(stream, GetRequestEncoding()))
			{
				using (var jsonReader = new JsonTextReader(streamReader))
				{
					var result = JsonExtensions.CreateDefaultJsonSerializer();

					return (T)result.Deserialize(jsonReader, typeof(T));
				}
			}
		}

		public async Task<RavenJObject> ReadJsonAsync()
		{
			using (var stream = await InnerRequest.Content.ReadAsStreamAsync())
			using (var streamReader = new StreamReader(stream, GetRequestEncoding()))
			using (var jsonReader = new RavenJsonTextReader(streamReader))
				return RavenJObject.Load(jsonReader);
		}

		public async Task<RavenJArray> ReadJsonArrayAsync()
		{
			using (var stream = await InnerRequest.Content.ReadAsStreamAsync())
			using (var streamReader = new StreamReader(stream, GetRequestEncoding()))
			using (var jsonReader = new RavenJsonTextReader(streamReader))
				return RavenJArray.Load(jsonReader);
		}

		public async Task<string> ReadStringAsync()
		{
			using (var stream = await InnerRequest.Content.ReadAsStreamAsync())
			using (var streamReader = new StreamReader(stream, GetRequestEncoding()))
				return streamReader.ReadToEnd();
		}

		public async Task<RavenJArray> ReadBsonArrayAsync()
		{
			using (var stream = await InnerRequest.Content.ReadAsStreamAsync())
			using (var jsonReader = new BsonReader(stream))
			{
				var jObject = RavenJObject.Load(jsonReader);
				return new RavenJArray(jObject.Values<RavenJToken>());
			}
		}

		private Encoding GetRequestEncoding()
		{
			if (InnerRequest.Content.Headers.ContentType == null || string.IsNullOrWhiteSpace(InnerRequest.Content.Headers.ContentType.CharSet))
				return Encoding.GetEncoding(Constants.DefaultRequestEncoding);
			return Encoding.GetEncoding(InnerRequest.Content.Headers.ContentType.CharSet);
		}

		public int GetStart()
		{
			int start;
			int.TryParse(GetQueryStringValue("start"), out start);
			return Math.Max(0, start);
		}

		public int GetPageSize(int maxPageSize)
		{
			int pageSize;
			if (int.TryParse(GetQueryStringValue("pageSize"), out pageSize) == false)
				pageSize = 25;
		    if (pageSize < 0)
		        return 0;
			if (pageSize > maxPageSize)
				pageSize = maxPageSize;
			return pageSize;
		}

		public bool MatchEtag(Etag etag)
		{
			return EtagHeaderToEtag() == etag;
		}

		internal Etag EtagHeaderToEtag()
		{
			var responseHeader = GetHeader("If-None-Match");
			if (string.IsNullOrEmpty(responseHeader))
				return Etag.InvalidEtag;

			if (responseHeader[0] == '\"')
				return Etag.Parse(responseHeader.Substring(1, responseHeader.Length - 2));

			return Etag.Parse(responseHeader);
		}

		public string GetQueryStringValue(string key)
		{
			return GetQueryStringValue(InnerRequest, key);
		}

		public static string GetQueryStringValue(HttpRequestMessage req, string key)
		{
			var value = req.GetQueryNameValuePairs().Where(pair => pair.Key == key).Select(pair => pair.Value).FirstOrDefault();
			if (value != null)
				value = Uri.UnescapeDataString(value);
			return value;
		}

		public string[] GetQueryStringValues(string key)
		{
			var items = InnerRequest.GetQueryNameValuePairs().Where(pair => pair.Key == key);
			return items.Select(pair => (pair.Value != null) ? Uri.UnescapeDataString(pair.Value) : null ).ToArray();
		}

		public Etag GetEtagFromQueryString()
		{
			var etagAsString = GetQueryStringValue("etag");
			return etagAsString != null ? Etag.Parse(etagAsString) : null;
		}

		public void WriteETag(Etag etag, HttpResponseMessage msg)
		{
			if (etag == null)
				return;
			WriteETag(etag.ToString(), msg);
		}

		public void WriteETag(string etag, HttpResponseMessage msg)
		{
			if (string.IsNullOrWhiteSpace(etag))
				return;

			msg.Headers.ETag = new EntityTagHeaderValue("\"" + etag + "\"");
		}

		public void WriteHeaders(RavenJObject headers, Etag etag, HttpResponseMessage msg)
		{
			foreach (var header in headers)
			{
				if (header.Key.StartsWith("@"))
					continue;

				switch (header.Key)
				{
					case "Content-Type":
						var headerValue = header.Value.Value<string>();
						string charset = null;
						if (headerValue.Contains("charset="))
						{
							var splits = headerValue.Split(';');
							headerValue = splits[0];

							charset = splits[1].Split('=')[1];
						}

						msg.Content.Headers.ContentType = new MediaTypeHeaderValue(headerValue) { CharSet = charset };

						break;
					default:
						if (header.Value.Type == JTokenType.Date)
						{
							var rfc1123 = GetDateString(header.Value, "r");
							var iso8601 = GetDateString(header.Value, "o");
							msg.Content.Headers.Add(header.Key, rfc1123);
							if (header.Key.StartsWith("Raven-") == false)
								msg.Content.Headers.Add("Raven-" + header.Key, iso8601);
						}
						else
						{
							var value = UnescapeStringIfNeeded(header.Value.ToString(Formatting.None));
							msg.Content.Headers.Add(header.Key, value);
						}
						break;
				}
			}
			if (headers["@Http-Status-Code"] != null)
			{
				msg.StatusCode = (HttpStatusCode)headers.Value<int>("@Http-Status-Code");
				msg.Content.Headers.Add("Temp-Status-Description", headers.Value<string>("@Http-Status-Description"));
			}

			WriteETag(etag, msg);
		}

		public void AddHeader(string key, string value, HttpResponseMessage msg)
		{
			if (msg.Content == null)
				msg.Content = JsonContent();

            // Ensure we haven't already appended these values.
            IEnumerable<string> existingValues;
            var hasExistingHeaderAppended = msg.Content.Headers.TryGetValues(key, out existingValues) && existingValues.Any(v => v == value);
            if (!hasExistingHeaderAppended)
            {
                msg.Content.Headers.Add(key, value);
            }
		}

		private string GetDateString(RavenJToken token, string format)
		{
			var value = token as RavenJValue;
			if (value == null)
				return token.ToString();

			var obj = value.Value;

			if (obj is DateTime)
				return ((DateTime)obj).ToString(format);

			if (obj is DateTimeOffset)
				return ((DateTimeOffset)obj).ToString(format);

			return obj.ToString();
		}

		private static string UnescapeStringIfNeeded(string str)
		{
			if (str.StartsWith("\"") && str.EndsWith("\""))
				str = Regex.Unescape(str.Substring(1, str.Length - 2));
			if (str.Any(ch => ch > 127))
			{
				// contains non ASCII chars, needs encoding
				return Uri.EscapeDataString(str);
			}
			return str;
		}

		public virtual HttpResponseMessage GetMessageWithObject(object item, HttpStatusCode code = HttpStatusCode.OK, Etag etag = null)
		{
			var token = item as RavenJToken;
			if (token == null && item != null)
			{
				token = RavenJToken.FromObject(item);
			}

            bool metadataOnly;
            if (bool.TryParse(GetQueryStringValue("metadata-only"), out metadataOnly) && metadataOnly)
                token = HttpExtensions.MinimizeToken(token);
            
			var msg = new HttpResponseMessage(code)
			{
				Content = JsonContent(token),
			};

			WriteETag(etag, msg);

			return msg;
		}

		public virtual HttpResponseMessage GetMessageWithString(string msg, HttpStatusCode code = HttpStatusCode.OK, Etag etag = null)
		{
			var resMsg = new HttpResponseMessage(code)
			{
				Content = JsonContent(msg)
			};

			WriteETag(etag, resMsg);

			return resMsg;
		}

		public virtual HttpResponseMessage GetEmptyMessage(HttpStatusCode code = HttpStatusCode.OK, Etag etag = null)
		{
			var resMsg = new HttpResponseMessage(code)
			{
				Content = JsonContent()
			};
			WriteETag(etag, resMsg);
			return resMsg;
		}

		public HttpResponseMessage WriteData(RavenJObject data, RavenJObject headers, Etag etag, HttpStatusCode status = HttpStatusCode.OK, HttpResponseMessage msg = null)
		{
			if (msg == null)
				msg = GetEmptyMessage(status);

			var jsonContent = ((JsonContent)msg.Content);

			WriteHeaders(headers, etag, msg);

			var jsonp = GetQueryStringValue("jsonp");
			if (string.IsNullOrEmpty(jsonp) == false)
				jsonContent.Jsonp = jsonp;

			jsonContent.Data = data;

			return msg;
		}

		public Etag GetEtag()
		{
			var etagAsString = GetHeader("If-None-Match") ?? GetHeader("If-Match");
			if (etagAsString != null)
			{
				// etags are usually quoted
				if (etagAsString.StartsWith("\"") && etagAsString.EndsWith("\""))
					etagAsString = etagAsString.Substring(1, etagAsString.Length - 2);

				Etag result;
				if (Etag.TryParse(etagAsString, out result))
					return result;

				throw new BadRequestException("Could not parse If-None-Match or If-Match header as Guid");
			}

			return null;
		}

		public string GetHeader(string key)
		{
			if (InnerHeaders.Contains(key) == false)
				return null;
			return InnerHeaders.GetValues(key).FirstOrDefault();
		}

		public List<string> GetHeaders(string key)
		{
			if (InnerHeaders.Contains(key) == false)
				return null;
			return InnerHeaders.GetValues(key).ToList();
		}

		public bool HasCookie(string key)
		{
			return InnerRequest.Headers.GetCookies(key).Count != 0;
		}

		public string GetCookie(string key)
		{
			var cookieHeaderValue = InnerRequest.Headers.GetCookies(key).FirstOrDefault();
			if (cookieHeaderValue != null)
			{
				var cookie = cookieHeaderValue.Cookies.FirstOrDefault();
				if (cookie != null)
					return cookie.Value;
			}

			return null;
		}

		public HttpResponseMessage WriteEmbeddedFile(string ravenPath, string embeddedPath, string zipPath,  string docPath)
		{
			var filePath = Path.Combine(ravenPath, docPath);
			if (File.Exists(filePath))
				return WriteFile(filePath);
			filePath = Path.Combine("~/../../../../Raven.Studio.Html5", docPath);
			if (File.Exists(filePath))
				return WriteFile(filePath);

			if (string.IsNullOrEmpty(zipPath) == false)
			{
			    var fullZipPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, zipPath + ".zip");
				if (File.Exists(fullZipPath) == false)
					fullZipPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", zipPath + ".zip");

				if (File.Exists(fullZipPath))
				{
                    return WriteFileFromZip(fullZipPath, docPath);
				}
			}

		    return WriteEmbeddedFileOfType(embeddedPath, docPath);
		}

		private HttpResponseMessage WriteFileFromZip(string zipPath, string docPath)
		{
			var etagValue = GetHeader("If-None-Match") ?? GetHeader("If-Match");
			var currentFileEtag = EmbeddedLastChangedDate + docPath;
			if (etagValue == currentFileEtag)
				return GetEmptyMessage(HttpStatusCode.NotModified);

			var fileStream = new FileStream(zipPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
			var zipFile = new ZipFile(fileStream);
			var zipEntry = zipFile.GetEntry(docPath);

			if (zipEntry == null || zipEntry.IsFile == false)
				return EmbeddedFileNotFound(docPath);

			var entry = zipFile.GetInputStream(zipEntry);
			var msg = new HttpResponseMessage
			{
				Content = new CompressedStreamContent(entry, false)
				{
					Disposables = {fileStream}
				},
			};

			WriteETag(currentFileEtag, msg);

			var type = GetContentType(docPath);
			msg.Content.Headers.ContentType = new MediaTypeHeaderValue(type);

			return msg;
		}

		public HttpResponseMessage WriteFile(string filePath)
		{
			var etagValue = GetHeader("If-None-Match") ?? GetHeader("If-None-Match");
			var fileEtag = File.GetLastWriteTimeUtc(filePath).ToString("G");
			if (etagValue == fileEtag)
				return GetEmptyMessage(HttpStatusCode.NotModified);

			var msg = new HttpResponseMessage
			{
				Content = new CompressedStreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), false)
			};

			WriteETag(fileEtag, msg);

			var type = GetContentType(filePath);
			msg.Content.Headers.ContentType = new MediaTypeHeaderValue(type);

			return msg;
		}

		private HttpResponseMessage WriteEmbeddedFileOfType(string embeddedPath, string docPath)
		{
			var etagValue = GetHeader("If-None-Match") ?? GetHeader("If-Match");
			var currentFileEtag = EmbeddedLastChangedDate + docPath;
			if (etagValue == currentFileEtag)
				return GetEmptyMessage(HttpStatusCode.NotModified);

			byte[] bytes;
			var resourceName = embeddedPath + "." + docPath.Replace("/", ".");

			var resourceAssembly = typeof(IHttpContext).Assembly;
			var resourceNames = resourceAssembly.GetManifestResourceNames();
			var lowercasedResourceName = resourceNames.FirstOrDefault(s => string.Equals(s, resourceName, StringComparison.OrdinalIgnoreCase));
		    if (lowercasedResourceName == null)
		    {
				return EmbeddedFileNotFound(docPath);
		    }
			using (var resource = resourceAssembly.GetManifestResourceStream(lowercasedResourceName))
			{
				if (resource == null)
					return EmbeddedFileNotFound(docPath);

				bytes = resource.ReadData();
			}
			var msg = new HttpResponseMessage
			{
				Content = new ByteArrayContent(bytes),
			};

			WriteETag(currentFileEtag, msg);

			var type = GetContentType(docPath);
			msg.Content.Headers.ContentType = new MediaTypeHeaderValue(type);

			return msg;
		}

		private HttpResponseMessage EmbeddedFileNotFound(string docPath)
		{
			var message = "The following embedded file was not available: " + docPath +
			              ". Please make sure that the Raven.Studio.Html.zip file exist in the main directory (near to the Raven.Database.dll).";
			return GetMessageWithObject(new {Message = message}, HttpStatusCode.NotFound);
		}

		private static readonly string EmbeddedLastChangedDate =
            File.GetLastWriteTime(AssemblyHelper.GetAssemblyLocationFor(typeof(HttpExtensions))).Ticks.ToString("G");

		private static string GetContentType(string docPath)
		{
			switch (Path.GetExtension(docPath))
			{
				case ".html":
				case ".htm":
					return "text/html";
				case ".css":
					return "text/css";
				case ".js":
					return "text/javascript";
				case ".ico":
					return "image/vnd.microsoft.icon";
				case ".jpg":
					return "image/jpeg";
				case ".gif":
					return "image/gif";
				case ".png":
					return "image/png";
				case ".xap":
					return "application/x-silverlight-2";
				default:
					return "text/plain";
			}
		}

		public class Headers : HttpHeaders
		{

		}

		public JsonContent JsonContent(RavenJToken data = null)
		{
			return new JsonContent(data)
				.WithRequest(InnerRequest);
		}

        public string GetRequestUrl()
        {
            var rawUrl = InnerRequest.RequestUri.PathAndQuery;
            return UrlExtension.GetRequestUrlFromRawUrl(rawUrl, SystemConfiguration);
        }

	    public abstract InMemoryRavenConfiguration SystemConfiguration { get; }


	    protected void AddRavenHeader(HttpResponseMessage msg, Stopwatch sp)
        {
            AddHeader("Raven-Server-Build", DocumentDatabase.BuildVersion, msg);
            AddHeader("Temp-Request-Time", sp.ElapsedMilliseconds.ToString("#,#;;0", CultureInfo.InvariantCulture), msg);
        }

	    public abstract bool SetupRequestToProperDatabase(RequestManager requestManager);

        public abstract string TenantName { get; }

        public List<Action<StringBuilder>> CustomRequestTraceInfo { get; private set; }

        public void AddRequestTraceInfo(Action<StringBuilder> info)
        {
            if (info == null)
                return;

            if (CustomRequestTraceInfo == null)
                CustomRequestTraceInfo = new List<Action<StringBuilder>>();
            
            CustomRequestTraceInfo.Add(info);
        }

	    public abstract void MarkRequestDuration(long duration);
	}
}
Back to Top