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
57 /// 短縮 URL の展開を無効にするか否か
59 public bool DisableExpanding { get; set; }
62 /// 短縮 URL のキャッシュを定期的にクリアする回数
64 public int PurgeCount { get; set; }
66 public string BitlyAccessToken { get; set; }
67 public string BitlyId { get; set; }
68 public string BitlyKey { get; set; }
70 private HttpClient http;
71 private ConcurrentDictionary<Uri, Uri> urlCache = new ConcurrentDictionary<Uri, Uri>();
73 private static readonly Regex HtmlLinkPattern = new Regex(@"(<a href="")(.+?)("")");
75 private static readonly HashSet<string> ShortUrlHosts = new HashSet<string>
92 "feeds.feedburner.com",
138 => _instance = new Lazy<ShortUrl>(() => new ShortUrl(), true);
140 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
142 : this(CreateDefaultHttpClient())
144 Networking.WebProxyChanged += (o, e) =>
146 var newClient = CreateDefaultHttpClient();
147 var oldClient = Interlocked.Exchange(ref this.http, newClient);
152 internal ShortUrl(HttpClient http)
154 this.DisableExpanding = false;
155 this.PurgeCount = 500;
163 public string ExpandUrl(string uri)
167 return this.ExpandUrlAsync(new Uri(uri), 10).Result.AbsoluteUri;
169 catch (UriFormatException)
176 /// 短縮 URL を非同期に展開します
178 /// <param name="uri">展開するURL</param>
179 /// <returns>URLの展開を行うタスク</returns>
180 public Task<Uri> ExpandUrlAsync(Uri uri)
181 => this.ExpandUrlAsync(uri, 10);
184 /// 短縮 URL を非同期に展開します
186 /// <param name="uri">展開するURL</param>
187 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
188 /// <returns>URLの展開を行うタスク</returns>
189 public async Task<Uri> ExpandUrlAsync(Uri uri, int redirectLimit)
191 if (this.DisableExpanding)
194 if (redirectLimit <= 0)
197 if (!uri.IsAbsoluteUri)
202 if (!ShortUrlHosts.Contains(uri.Host) && !IsIrregularShortUrl(uri))
205 if (this.urlCache.TryGetValue(uri, out var expanded))
208 if (this.urlCache.Count > this.PurgeCount)
209 this.urlCache.Clear();
214 expanded = await this.GetRedirectTo(uri)
215 .ConfigureAwait(false);
217 catch (TaskCanceledException) { }
218 catch (HttpRequestException) { }
220 if (expanded == null || expanded == uri)
223 this.urlCache[uri] = expanded;
225 var recursiveExpanded = await this.ExpandUrlAsync(expanded, --redirectLimit)
226 .ConfigureAwait(false);
228 // URL1 -> URL2 -> URL3 のように再帰的に展開されたURLを URL1 -> URL3 としてキャッシュに格納する
229 if (recursiveExpanded != expanded)
230 this.urlCache[uri] = recursiveExpanded;
232 return recursiveExpanded;
234 catch (UriFormatException)
241 /// 短縮 URL を非同期に展開します
244 /// 不正なURLが渡された場合は例外を投げず uriStr をそのまま返します
246 /// <param name="uriStr">展開するURL</param>
247 /// <returns>URLの展開を行うタスク</returns>
248 public Task<string> ExpandUrlAsync(string uriStr)
249 => this.ExpandUrlAsync(uriStr, 10);
252 /// 短縮 URL を非同期に展開します
255 /// 不正なURLが渡された場合は例外を投げず uriStr をそのまま返します
257 /// <param name="uriStr">展開するURL</param>
258 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
259 /// <returns>URLの展開を行うタスク</returns>
260 public async Task<string> ExpandUrlAsync(string uriStr, int redirectLimit)
266 if (!uriStr.StartsWith("http", StringComparison.OrdinalIgnoreCase))
267 uri = new Uri("http://" + uriStr);
269 uri = new Uri(uriStr);
271 catch (UriFormatException)
276 var expandedUri = await this.ExpandUrlAsync(uri, redirectLimit)
277 .ConfigureAwait(false);
279 return expandedUri.OriginalString;
283 public string ExpandUrlHtml(string html)
284 => this.ExpandUrlHtmlAsync(html, 10).Result;
287 /// HTML内に含まれるリンクのURLを非同期に展開する
289 /// <param name="html">処理対象のHTML</param>
290 /// <returns>URLの展開を行うタスク</returns>
291 public Task<string> ExpandUrlHtmlAsync(string html)
292 => this.ExpandUrlHtmlAsync(html, 10);
295 /// HTML内に含まれるリンクのURLを非同期に展開する
297 /// <param name="html">処理対象のHTML</param>
298 /// <param name="redirectLimit">再帰的に展開を試みる上限</param>
299 /// <returns>URLの展開を行うタスク</returns>
300 public Task<string> ExpandUrlHtmlAsync(string html, int redirectLimit)
302 if (this.DisableExpanding)
303 return Task.FromResult(html);
305 return HtmlLinkPattern.ReplaceAsync(html, async m =>
306 m.Groups[1].Value + await this.ExpandUrlAsync(m.Groups[2].Value, redirectLimit).ConfigureAwait(false) + m.Groups[3].Value);
310 /// 指定された短縮URLサービスを使用してURLを短縮します
312 /// <param name="shortenerType">使用する短縮URLサービス</param>
313 /// <param name="srcUri">短縮するURL</param>
314 /// <returns>短縮されたURL</returns>
315 public async Task<Uri> ShortenUrlAsync(MyCommon.UrlConverter shortenerType, Uri srcUri)
317 // 既に短縮されている状態のURLであれば短縮しない
318 if (ShortUrlHosts.Contains(srcUri.Host))
323 switch (shortenerType)
325 case MyCommon.UrlConverter.TinyUrl:
326 return await this.ShortenByTinyUrlAsync(srcUri)
327 .ConfigureAwait(false);
328 case MyCommon.UrlConverter.Isgd:
329 return await this.ShortenByIsgdAsync(srcUri)
330 .ConfigureAwait(false);
331 case MyCommon.UrlConverter.Twurl:
332 return await this.ShortenByTwurlAsync(srcUri)
333 .ConfigureAwait(false);
334 case MyCommon.UrlConverter.Bitly:
335 return await this.ShortenByBitlyAsync(srcUri, "bit.ly")
336 .ConfigureAwait(false);
337 case MyCommon.UrlConverter.Jmp:
338 return await this.ShortenByBitlyAsync(srcUri, "j.mp")
339 .ConfigureAwait(false);
340 case MyCommon.UrlConverter.Uxnu:
341 return await this.ShortenByUxnuAsync(srcUri)
342 .ConfigureAwait(false);
344 throw new ArgumentException("Unknown shortener.", nameof(shortenerType));
347 catch (OperationCanceledException)
349 // 短縮 URL の API がタイムアウトした場合
354 private async Task<Uri> ShortenByTinyUrlAsync(Uri srcUri)
356 // 明らかに長くなると推測できる場合は短縮しない
357 if ("http://tinyurl.com/xxxxxx".Length > srcUri.OriginalString.Length)
360 var content = new FormUrlEncodedContent(new[]
362 new KeyValuePair<string, string>("url", srcUri.OriginalString),
365 using (var response = await this.http.PostAsync("http://tinyurl.com/api-create.php", content).ConfigureAwait(false))
367 response.EnsureSuccessStatusCode();
369 var result = await response.Content.ReadAsStringAsync()
370 .ConfigureAwait(false);
372 if (!Regex.IsMatch(result, @"^https?://"))
373 throw new WebApiException("Failed to create URL.", result);
375 return new Uri(result.TrimEnd());
379 private async Task<Uri> ShortenByIsgdAsync(Uri srcUri)
381 // 明らかに長くなると推測できる場合は短縮しない
382 if ("http://is.gd/xxxx".Length > srcUri.OriginalString.Length)
385 var content = new FormUrlEncodedContent(new[]
387 new KeyValuePair<string, string>("format", "simple"),
388 new KeyValuePair<string, string>("url", srcUri.OriginalString),
391 using (var response = await this.http.PostAsync("http://is.gd/create.php", content).ConfigureAwait(false))
393 response.EnsureSuccessStatusCode();
395 var result = await response.Content.ReadAsStringAsync()
396 .ConfigureAwait(false);
398 if (!Regex.IsMatch(result, @"^https?://"))
399 throw new WebApiException("Failed to create URL.", result);
401 return new Uri(result.TrimEnd());
405 private async Task<Uri> ShortenByTwurlAsync(Uri srcUri)
407 // 明らかに長くなると推測できる場合は短縮しない
408 if ("http://twurl.nl/xxxxxx".Length > srcUri.OriginalString.Length)
411 var content = new FormUrlEncodedContent(new[]
413 new KeyValuePair<string, string>("link[url]", srcUri.OriginalString),
416 using (var response = await this.http.PostAsync("http://tweetburner.com/links", content).ConfigureAwait(false))
418 response.EnsureSuccessStatusCode();
420 var result = await response.Content.ReadAsStringAsync()
421 .ConfigureAwait(false);
423 if (!Regex.IsMatch(result, @"^https?://"))
424 throw new WebApiException("Failed to create URL.", result);
426 return new Uri(result.TrimEnd());
430 private async Task<Uri> ShortenByBitlyAsync(Uri srcUri, string domain = "bit.ly")
432 // 明らかに長くなると推測できる場合は短縮しない
433 if ("http://bit.ly/xxxx".Length > srcUri.OriginalString.Length)
436 // OAuth2 アクセストークンまたは API キー (旧方式) のいずれも設定されていなければ短縮しない
437 if (string.IsNullOrEmpty(this.BitlyAccessToken) && (string.IsNullOrEmpty(this.BitlyId) || string.IsNullOrEmpty(this.BitlyKey)))
440 var bitly = new BitlyApi
442 EndUserAccessToken = this.BitlyAccessToken,
443 EndUserLoginName = this.BitlyId,
444 EndUserApiKey = this.BitlyKey,
447 return await bitly.ShortenAsync(srcUri, domain)
448 .ConfigureAwait(false);
451 private async Task<Uri> ShortenByUxnuAsync(Uri srcUri)
453 // 明らかに長くなると推測できる場合は短縮しない
454 if ("http://ux.nx/xxxxxx".Length > srcUri.OriginalString.Length)
457 var query = new Dictionary<string, string>
459 ["format"] = "plain",
460 ["url"] = srcUri.OriginalString,
463 var uri = new Uri("http://ux.nu/api/short?" + MyCommon.BuildQueryString(query));
464 using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
466 response.EnsureSuccessStatusCode();
468 var result = await response.Content.ReadAsStringAsync()
469 .ConfigureAwait(false);
471 if (!Regex.IsMatch(result, @"^https?://"))
472 throw new WebApiException("Failed to create URL.", result);
474 return new Uri(result.TrimEnd());
478 private bool IsIrregularShortUrl(Uri uri)
480 // Flickrの https://www.flickr.com/photo.gne?short=... 形式のURL
481 // flic.kr ドメインのURLを展開する途中に経由する
482 if (uri.Host.EndsWith("flickr.com", StringComparison.OrdinalIgnoreCase) &&
483 uri.PathAndQuery.StartsWith("/photo.gne", StringComparison.OrdinalIgnoreCase))
489 private async Task<Uri> GetRedirectTo(Uri url)
491 var request = new HttpRequestMessage(HttpMethod.Head, url);
493 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
495 if (!response.IsSuccessStatusCode)
497 // ステータスコードが 3xx であれば例外を発生させない
498 if ((int)response.StatusCode / 100 != 3)
499 response.EnsureSuccessStatusCode();
502 var redirectedUrl = response.Headers.Location;
504 if (redirectedUrl == null)
507 // サーバーが URL を適切にエンコードしていない場合、OriginalString に非 ASCII 文字が含まれる。
508 // その場合、redirectedUrl は文字化けしている可能性があるため使用しない
509 // 参照: http://stackoverflow.com/questions/1888933
510 if (redirectedUrl.OriginalString.Any(x => x < ' ' || x > '~'))
513 if (redirectedUrl.IsAbsoluteUri)
514 return redirectedUrl;
516 return new Uri(url, redirectedUrl);
520 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
521 private static HttpClient CreateDefaultHttpClient()
523 var handler = Networking.CreateHttpClientHandler();
524 handler.AllowAutoRedirect = false;
526 var http = Networking.CreateHttpClient(handler);
527 http.Timeout = TimeSpan.FromSeconds(30);