1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
22 // You should have received a copy of the GNU General public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Concurrent;
29 using System.Collections.Generic;
30 using System.Diagnostics.CodeAnalysis;
32 using System.Net.Http;
34 using System.Text.RegularExpressions;
35 using System.Threading;
36 using System.Threading.Tasks;
39 using OpenTween.Connection;
44 /// 短縮 URL サービスによる URL の展開・短縮を行うクラス
48 private static Lazy<ShortUrl> _instance;
51 /// ShortUrl のインスタンスを取得します
53 public static ShortUrl Instance
55 get { return _instance.Value; }
59 /// 短縮 URL の展開を無効にするか否か
61 public bool DisableExpanding { get; set; }
64 /// 短縮 URL のキャッシュを定期的にクリアする回数
66 public int PurgeCount { get; set; }
68 public string BitlyAccessToken { get; set; }
69 public string BitlyId { get; set; }
70 public string BitlyKey { get; set; }
72 private HttpClient http;
73 private ConcurrentDictionary<Uri, Uri> urlCache = new ConcurrentDictionary<Uri, Uri>();
75 private static readonly Regex HtmlLinkPattern = new Regex(@"(<a href="")(.+?)("")");
77 private static readonly HashSet<string> ShortUrlHosts = new HashSet<string>
94 "feeds.feedburner.com",
141 _instance = new Lazy<ShortUrl>(() => new ShortUrl(), true);
144 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
146 : this(CreateDefaultHttpClient())
148 Networking.WebProxyChanged += (o, e) =>
150 var newClient = CreateDefaultHttpClient();
151 var oldClient = Interlocked.Exchange(ref this.http, newClient);
156 internal ShortUrl(HttpClient http)
158 this.DisableExpanding = false;
159 this.PurgeCount = 500;
167 public string ExpandUrl(string uri)
171 return this.ExpandUrlAsync(new Uri(uri), 10).Result.AbsoluteUri;
173 catch (UriFormatException)
180 /// 短縮 URL を非同期に展開します
182 /// <param name="uri">展開するURL</param>
183 /// <returns>URLの展開を行うタスク</returns>
184 public Task<Uri> ExpandUrlAsync(Uri uri)
186 return this.ExpandUrlAsync(uri, 10);
190 /// 短縮 URL を非同期に展開します
192 /// <param name="uri">展開するURL</param>
193 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
194 /// <returns>URLの展開を行うタスク</returns>
195 public async Task<Uri> ExpandUrlAsync(Uri uri, int redirectLimit)
197 if (this.DisableExpanding)
200 if (redirectLimit <= 0)
203 if (!uri.IsAbsoluteUri)
208 if (!ShortUrlHosts.Contains(uri.Host) && !IsIrregularShortUrl(uri))
211 if (this.urlCache.TryGetValue(uri, out var expanded))
214 if (this.urlCache.Count > this.PurgeCount)
215 this.urlCache.Clear();
220 expanded = await this.GetRedirectTo(uri)
221 .ConfigureAwait(false);
223 catch (TaskCanceledException) { }
224 catch (HttpRequestException) { }
226 if (expanded == null || expanded == uri)
229 this.urlCache[uri] = expanded;
231 var recursiveExpanded = await this.ExpandUrlAsync(expanded, --redirectLimit)
232 .ConfigureAwait(false);
234 // URL1 -> URL2 -> URL3 のように再帰的に展開されたURLを URL1 -> URL3 としてキャッシュに格納する
235 if (recursiveExpanded != expanded)
236 this.urlCache[uri] = recursiveExpanded;
238 return recursiveExpanded;
240 catch (UriFormatException)
247 /// 短縮 URL を非同期に展開します
250 /// 不正なURLが渡された場合は例外を投げず uriStr をそのまま返します
252 /// <param name="uriStr">展開するURL</param>
253 /// <returns>URLの展開を行うタスク</returns>
254 public Task<string> ExpandUrlAsync(string uriStr)
256 return this.ExpandUrlAsync(uriStr, 10);
260 /// 短縮 URL を非同期に展開します
263 /// 不正なURLが渡された場合は例外を投げず uriStr をそのまま返します
265 /// <param name="uriStr">展開するURL</param>
266 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
267 /// <returns>URLの展開を行うタスク</returns>
268 public async Task<string> ExpandUrlAsync(string uriStr, int redirectLimit)
274 if (!uriStr.StartsWith("http", StringComparison.OrdinalIgnoreCase))
275 uri = new Uri("http://" + uriStr);
277 uri = new Uri(uriStr);
279 catch (UriFormatException)
284 var expandedUri = await this.ExpandUrlAsync(uri, redirectLimit)
285 .ConfigureAwait(false);
287 return expandedUri.OriginalString;
291 public string ExpandUrlHtml(string html)
293 return this.ExpandUrlHtmlAsync(html, 10).Result;
297 /// HTML内に含まれるリンクのURLを非同期に展開する
299 /// <param name="html">処理対象のHTML</param>
300 /// <returns>URLの展開を行うタスク</returns>
301 public Task<string> ExpandUrlHtmlAsync(string html)
303 return this.ExpandUrlHtmlAsync(html, 10);
307 /// HTML内に含まれるリンクのURLを非同期に展開する
309 /// <param name="html">処理対象のHTML</param>
310 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
311 /// <returns>URLの展開を行うタスク</returns>
312 public Task<string> ExpandUrlHtmlAsync(string html, int redirectLimit)
314 if (this.DisableExpanding)
315 return Task.FromResult(html);
317 return HtmlLinkPattern.ReplaceAsync(html, async m =>
318 m.Groups[1].Value + await this.ExpandUrlAsync(m.Groups[2].Value, redirectLimit).ConfigureAwait(false) + m.Groups[3].Value);
322 /// 指定された短縮URLサービスを使用してURLを短縮します
324 /// <param name="shortenerType">使用する短縮URLサービス</param>
325 /// <param name="srcUri">短縮するURL</param>
326 /// <returns>短縮されたURL</returns>
327 public async Task<Uri> ShortenUrlAsync(MyCommon.UrlConverter shortenerType, Uri srcUri)
329 // 既に短縮されている状態のURLであれば短縮しない
330 if (ShortUrlHosts.Contains(srcUri.Host))
335 switch (shortenerType)
337 case MyCommon.UrlConverter.TinyUrl:
338 return await this.ShortenByTinyUrlAsync(srcUri)
339 .ConfigureAwait(false);
340 case MyCommon.UrlConverter.Isgd:
341 return await this.ShortenByIsgdAsync(srcUri)
342 .ConfigureAwait(false);
343 case MyCommon.UrlConverter.Twurl:
344 return await this.ShortenByTwurlAsync(srcUri)
345 .ConfigureAwait(false);
346 case MyCommon.UrlConverter.Bitly:
347 return await this.ShortenByBitlyAsync(srcUri, "bit.ly")
348 .ConfigureAwait(false);
349 case MyCommon.UrlConverter.Jmp:
350 return await this.ShortenByBitlyAsync(srcUri, "j.mp")
351 .ConfigureAwait(false);
352 case MyCommon.UrlConverter.Uxnu:
353 return await this.ShortenByUxnuAsync(srcUri)
354 .ConfigureAwait(false);
356 throw new ArgumentException("Unknown shortener.", nameof(shortenerType));
359 catch (OperationCanceledException)
361 // 短縮 URL の API がタイムアウトした場合
366 private async Task<Uri> ShortenByTinyUrlAsync(Uri srcUri)
368 // 明らかに長くなると推測できる場合は短縮しない
369 if ("http://tinyurl.com/xxxxxx".Length > srcUri.OriginalString.Length)
372 var content = new FormUrlEncodedContent(new[]
374 new KeyValuePair<string, string>("url", srcUri.OriginalString),
377 using (var response = await this.http.PostAsync("http://tinyurl.com/api-create.php", content).ConfigureAwait(false))
379 response.EnsureSuccessStatusCode();
381 var result = await response.Content.ReadAsStringAsync()
382 .ConfigureAwait(false);
384 if (!Regex.IsMatch(result, @"^https?://"))
385 throw new WebApiException("Failed to create URL.", result);
387 return new Uri(result.TrimEnd());
391 private async Task<Uri> ShortenByIsgdAsync(Uri srcUri)
393 // 明らかに長くなると推測できる場合は短縮しない
394 if ("http://is.gd/xxxx".Length > srcUri.OriginalString.Length)
397 var content = new FormUrlEncodedContent(new[]
399 new KeyValuePair<string, string>("format", "simple"),
400 new KeyValuePair<string, string>("url", srcUri.OriginalString),
403 using (var response = await this.http.PostAsync("http://is.gd/create.php", content).ConfigureAwait(false))
405 response.EnsureSuccessStatusCode();
407 var result = await response.Content.ReadAsStringAsync()
408 .ConfigureAwait(false);
410 if (!Regex.IsMatch(result, @"^https?://"))
411 throw new WebApiException("Failed to create URL.", result);
413 return new Uri(result.TrimEnd());
417 private async Task<Uri> ShortenByTwurlAsync(Uri srcUri)
419 // 明らかに長くなると推測できる場合は短縮しない
420 if ("http://twurl.nl/xxxxxx".Length > srcUri.OriginalString.Length)
423 var content = new FormUrlEncodedContent(new[]
425 new KeyValuePair<string, string>("link[url]", srcUri.OriginalString),
428 using (var response = await this.http.PostAsync("http://tweetburner.com/links", content).ConfigureAwait(false))
430 response.EnsureSuccessStatusCode();
432 var result = await response.Content.ReadAsStringAsync()
433 .ConfigureAwait(false);
435 if (!Regex.IsMatch(result, @"^https?://"))
436 throw new WebApiException("Failed to create URL.", result);
438 return new Uri(result.TrimEnd());
442 private async Task<Uri> ShortenByBitlyAsync(Uri srcUri, string domain = "bit.ly")
444 // 明らかに長くなると推測できる場合は短縮しない
445 if ("http://bit.ly/xxxx".Length > srcUri.OriginalString.Length)
448 // OAuth2 アクセストークンまたは API キー (旧方式) のいずれも設定されていなければ短縮しない
449 if (string.IsNullOrEmpty(this.BitlyAccessToken) && (string.IsNullOrEmpty(this.BitlyId) || string.IsNullOrEmpty(this.BitlyKey)))
452 var bitly = new BitlyApi
454 EndUserAccessToken = this.BitlyAccessToken,
455 EndUserLoginName = this.BitlyId,
456 EndUserApiKey = this.BitlyKey,
459 return await bitly.ShortenAsync(srcUri, domain)
460 .ConfigureAwait(false);
463 private async Task<Uri> ShortenByUxnuAsync(Uri srcUri)
465 // 明らかに長くなると推測できる場合は短縮しない
466 if ("http://ux.nx/xxxxxx".Length > srcUri.OriginalString.Length)
469 var query = new Dictionary<string, string>
471 ["format"] = "plain",
472 ["url"] = srcUri.OriginalString,
475 var uri = new Uri("http://ux.nu/api/short?" + MyCommon.BuildQueryString(query));
476 using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
478 response.EnsureSuccessStatusCode();
480 var result = await response.Content.ReadAsStringAsync()
481 .ConfigureAwait(false);
483 if (!Regex.IsMatch(result, @"^https?://"))
484 throw new WebApiException("Failed to create URL.", result);
486 return new Uri(result.TrimEnd());
490 private bool IsIrregularShortUrl(Uri uri)
492 // Flickrの https://www.flickr.com/photo.gne?short=... 形式のURL
493 // flic.kr ドメインのURLを展開する途中に経由する
494 if (uri.Host.EndsWith("flickr.com", StringComparison.OrdinalIgnoreCase) &&
495 uri.PathAndQuery.StartsWith("/photo.gne", StringComparison.OrdinalIgnoreCase))
501 private async Task<Uri> GetRedirectTo(Uri url)
503 var request = new HttpRequestMessage(HttpMethod.Head, url);
505 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
507 if (!response.IsSuccessStatusCode)
509 // ステータスコードが 3xx であれば例外を発生させない
510 if ((int)response.StatusCode / 100 != 3)
511 response.EnsureSuccessStatusCode();
514 var redirectedUrl = response.Headers.Location;
516 if (redirectedUrl == null)
519 // サーバーが URL を適切にエンコードしていない場合、OriginalString に非 ASCII 文字が含まれる。
520 // その場合、redirectedUrl は文字化けしている可能性があるため使用しない
521 // 参照: http://stackoverflow.com/questions/1888933
522 if (redirectedUrl.OriginalString.Any(x => x < ' ' || x > '~'))
525 if (redirectedUrl.IsAbsoluteUri)
526 return redirectedUrl;
528 return new Uri(url, redirectedUrl);
532 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
533 private static HttpClient CreateDefaultHttpClient()
535 var handler = Networking.CreateHttpClientHandler();
536 handler.AllowAutoRedirect = false;
538 var http = Networking.CreateHttpClient(handler);
539 http.Timeout = TimeSpan.FromSeconds(30);