Commit eb5749ed392aa375ee6c50e9f6c9a5aabc11820b
1 parent
cc836914
Exists in
master
and in
4 other branches
Fixed encoding problems with plain/text blobs being sent without charset.
Showing
2 changed files
with
19 additions
and
4 deletions
Show diff stats
app/controllers/refs_controller.rb
| 1 | class RefsController < ApplicationController | 1 | class RefsController < ApplicationController |
| 2 | + include Gitlabhq::Encode | ||
| 2 | before_filter :project | 3 | before_filter :project |
| 3 | 4 | ||
| 4 | # Authorize | 5 | # Authorize |
| @@ -49,9 +50,16 @@ class RefsController < ApplicationController | @@ -49,9 +50,16 @@ class RefsController < ApplicationController | ||
| 49 | 50 | ||
| 50 | def blob | 51 | def blob |
| 51 | if @tree.is_blob? | 52 | if @tree.is_blob? |
| 53 | + if @tree.text? | ||
| 54 | + encoding = detect_encoding(@tree.data) | ||
| 55 | + mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain" | ||
| 56 | + else | ||
| 57 | + mime_type = @tree.mime_type | ||
| 58 | + end | ||
| 59 | + | ||
| 52 | send_data( | 60 | send_data( |
| 53 | @tree.data, | 61 | @tree.data, |
| 54 | - :type => @tree.text? ? "text/plain" : @tree.mime_type, | 62 | + :type => mime_type, |
| 55 | :disposition => 'inline', | 63 | :disposition => 'inline', |
| 56 | :filename => @tree.name | 64 | :filename => @tree.name |
| 57 | ) | 65 | ) |
lib/gitlabhq/encode.rb
| @@ -5,9 +5,9 @@ module Gitlabhq | @@ -5,9 +5,9 @@ module Gitlabhq | ||
| 5 | def utf8 message | 5 | def utf8 message |
| 6 | return nil unless message | 6 | return nil unless message |
| 7 | 7 | ||
| 8 | - hash = CharlockHolmes::EncodingDetector.detect(message) rescue {} | ||
| 9 | - if hash[:encoding] | ||
| 10 | - CharlockHolmes::Converter.convert(message, hash[:encoding], 'UTF-8') | 8 | + encoding = detect_encoding(message) |
| 9 | + if encoding | ||
| 10 | + CharlockHolmes::Converter.convert(message, encoding, 'UTF-8') | ||
| 11 | else | 11 | else |
| 12 | message | 12 | message |
| 13 | end.force_encoding("utf-8") | 13 | end.force_encoding("utf-8") |
| @@ -16,5 +16,12 @@ module Gitlabhq | @@ -16,5 +16,12 @@ module Gitlabhq | ||
| 16 | rescue | 16 | rescue |
| 17 | "" | 17 | "" |
| 18 | end | 18 | end |
| 19 | + | ||
| 20 | + def detect_encoding message | ||
| 21 | + return nil unless message | ||
| 22 | + | ||
| 23 | + hash = CharlockHolmes::EncodingDetector.detect(message) rescue {} | ||
| 24 | + return hash[:encoding] ? hash[:encoding] : nil | ||
| 25 | + end | ||
| 19 | end | 26 | end |
| 20 | end | 27 | end |