Class: Gitlab::HttpIO

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/http_io.rb

Constant Summary collapse

BUFFER_SIZE =
128.kilobytes
InvalidURLError =
Class.new(StandardError)
FailedToGetChunkError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, size) ⇒ HttpIO

Returns a new instance of HttpIO.

Raises:



19
20
21
22
23
24
25
# File 'lib/gitlab/http_io.rb', line 19

def initialize(url, size)
  raise InvalidURLError unless ::Gitlab::UrlSanitizer.valid?(url)

  @uri = URI(url)
  @size = size
  @tell = 0
end

Instance Attribute Details

#chunkObject (readonly)

Returns the value of attribute chunk.



15
16
17
# File 'lib/gitlab/http_io.rb', line 15

def chunk
  @chunk
end

#chunk_rangeObject (readonly)

Returns the value of attribute chunk_range.



15
16
17
# File 'lib/gitlab/http_io.rb', line 15

def chunk_range
  @chunk_range
end

#sizeObject (readonly)

Returns the value of attribute size.



13
14
15
# File 'lib/gitlab/http_io.rb', line 13

def size
  @size
end

#tellObject (readonly) Also known as: pos

Returns the value of attribute tell.



14
15
16
# File 'lib/gitlab/http_io.rb', line 14

def tell
  @tell
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/gitlab/http_io.rb', line 13

def uri
  @uri
end

Instance Method Details

#binmodeObject



31
32
33
# File 'lib/gitlab/http_io.rb', line 31

def binmode
  # no-op
end

#binmode?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/gitlab/http_io.rb', line 35

def binmode?
  true
end

#closeObject



27
28
29
# File 'lib/gitlab/http_io.rb', line 27

def close
  # no-op
end

#each_lineObject



69
70
71
72
73
74
75
76
# File 'lib/gitlab/http_io.rb', line 69

def each_line
  until eof?
    line = readline
    break if line.nil?

    yield(line)
  end
end

#eof?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/gitlab/http_io.rb', line 65

def eof?
  tell == size
end

#flushObject

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/gitlab/http_io.rb', line 133

def flush
  raise NotImplementedError
end

#pathObject



39
40
41
# File 'lib/gitlab/http_io.rb', line 39

def path
  nil
end

#present?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/gitlab/http_io.rb', line 137

def present?
  true
end

#read(length = nil, outbuf = nil) ⇒ Object



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
# File 'lib/gitlab/http_io.rb', line 78

def read(length = nil, outbuf = nil)
  out = []

  length ||= size - tell

  until length <= 0 || eof?
    data = get_chunk
    break if data.empty?

    chunk_bytes = [BUFFER_SIZE - chunk_offset, length].min
    data_slice = data.byteslice(0, chunk_bytes)

    out << data_slice
    @tell += data_slice.bytesize
    length -= data_slice.bytesize
  end

  out = out.join

  # If outbuf is passed, we put the output into the buffer. This supports IO.copy_stream functionality
  if outbuf
    outbuf.replace(out)
  end

  out
end

#readlineObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gitlab/http_io.rb', line 105

def readline
  out = []

  until eof?
    data = get_chunk
    new_line = data.index("\n")

    if !new_line.nil?
      out << data[0..new_line]
      @tell += new_line + 1
      break
    else
      out << data
      @tell += data.bytesize
    end
  end

  out.join
end

#seek(pos, where = IO::SEEK_SET) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/http_io.rb', line 47

def seek(pos, where = IO::SEEK_SET)
  new_pos =
    case where
    when IO::SEEK_END
      size + pos
    when IO::SEEK_SET
      pos
    when IO::SEEK_CUR
      tell + pos
    else
      -1
    end

  raise 'new position is outside of file' if new_pos < 0 || new_pos > size

  @tell = new_pos
end

#truncate(offset) ⇒ Object

Raises:

  • (NotImplementedError)


129
130
131
# File 'lib/gitlab/http_io.rb', line 129

def truncate(offset)
  raise NotImplementedError
end

#urlObject



43
44
45
# File 'lib/gitlab/http_io.rb', line 43

def url
  @uri.to_s
end

#write(data) ⇒ Object

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/gitlab/http_io.rb', line 125

def write(data)
  raise NotImplementedError
end