22package main
33
44import (
5+ "bytes"
56 "context"
67 "log"
78 "os"
@@ -78,7 +79,7 @@ type ParsedParts struct {
7879 ip net.IP
7980 file string
8081 port int
81- length int64
82+ length uint64
8283 receiverNick string
8384 senderNick string
8485 serverHostname string
@@ -92,7 +93,7 @@ func parseSendParams(text string) *ParsedParts {
9293 //parts := text.match(/(?:[^\s"]+|"[^"]*")+/g);
9394 ipInt , _ := strconv .ParseUint (parts [3 ], 10 , 32 )
9495 portInt , _ := strconv .ParseInt (parts [4 ], 10 , 0 )
95- lengthInt , _ := strconv .ParseInt (parts [5 ], 10 , 64 )
96+ lengthInt , _ := strconv .ParseUint (parts [5 ], 10 , 64 )
9697 partsStruct := & ParsedParts {
9798 file : parts [2 ], /*.replace(/^"(.+)"$/, '$1')*/
9899 ip : int2ip (uint32 (ipInt )),
@@ -122,12 +123,27 @@ func ensureUtf8(s string, fromEncoding string) string {
122123type WriteCounter struct {
123124 Total uint64
124125 connection * net.Conn
126+ expectedLength uint64
127+ }
128+ func reverseBytes (input []byte ) []byte {
129+ if len (input ) == 0 {
130+ return input
131+ }
132+ return append (reverseBytes (input [1 :]), input [0 ])
125133}
126-
127134func (wc * WriteCounter ) Write (p []byte ) (int , error ) {
128135 n := len (p )
129136 wc .Total += uint64 (n )
130- binary .Write ((* wc .connection ), binary .BigEndian , wc .Total )
137+ buf := bytes .NewBuffer ([]byte {})
138+
139+ if wc .expectedLength > 0xffffffff {
140+ binary .Write ((* wc .connection ), binary .BigEndian , buf .Bytes ())
141+
142+ }else {
143+ binary .Write (buf , binary .LittleEndian , reverseBytes (buf .Bytes ())[0 :4 ])
144+
145+ }
146+
131147 return n , nil
132148}
133149
@@ -155,14 +171,15 @@ func serveFile(parts ParsedParts, w http.ResponseWriter, r *http.Request) (work
155171 counter := & WriteCounter {
156172 connection :& conn ,
157173 Total : 0 ,
174+ expectedLength : parts .length ,
158175 }
159176
160177
161178 contentDisposition := fmt .Sprintf ("attachment; filename=%s" , parts .file )
162179 w .Header ().Set ("Content-Disposition" , contentDisposition )
163180 w .Header ().Set ("Content-Type" , "application/octet-stream" )
164181 intLength := int (parts .length )
165- if int64 (intLength ) != parts .length {
182+ if uint64 (intLength ) != parts .length {
166183 panic ("overflows!" )
167184 }
168185 w .Header ().Set ("Content-Length" , strconv .Itoa (intLength ) /*r.Header.Get("Content-Length")*/ )
0 commit comments