Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
796cb86b
authored
Apr 25, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fixing SSE support
parent
bbaeea93
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
20 deletions
+53
-20
controller/relay.go
+53
-20
No files found.
controller/relay.go
View file @
796cb86b
package
controller
package
controller
import
(
import
(
"bufio"
"bytes"
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"io"
"io"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"strings"
)
)
func
Relay
(
c
*
gin
.
Context
)
{
func
Relay
(
c
*
gin
.
Context
)
{
...
@@ -30,18 +33,8 @@ func Relay(c *gin.Context) {
...
@@ -30,18 +33,8 @@ func Relay(c *gin.Context) {
//req.Header.Del("Accept-Encoding")
//req.Header.Del("Accept-Encoding")
req
.
Header
.
Set
(
"Authorization"
,
c
.
Request
.
Header
.
Get
(
"Authorization"
))
req
.
Header
.
Set
(
"Authorization"
,
c
.
Request
.
Header
.
Get
(
"Authorization"
))
req
.
Header
.
Set
(
"Content-Type"
,
c
.
Request
.
Header
.
Get
(
"Content-Type"
))
req
.
Header
.
Set
(
"Content-Type"
,
c
.
Request
.
Header
.
Get
(
"Content-Type"
))
acceptHeader
:=
c
.
Request
.
Header
.
Get
(
"Accept"
)
req
.
Header
.
Set
(
"Accept"
,
c
.
Request
.
Header
.
Get
(
"Accept"
))
if
acceptHeader
!=
""
{
req
.
Header
.
Set
(
"Connection"
,
c
.
Request
.
Header
.
Get
(
"Connection"
))
req
.
Header
.
Set
(
"Accept"
,
acceptHeader
)
}
connectionHeader
:=
c
.
Request
.
Header
.
Get
(
"Connection"
)
if
connectionHeader
!=
""
{
req
.
Header
.
Set
(
"Connection"
,
connectionHeader
)
}
lastEventIDHeader
:=
c
.
Request
.
Header
.
Get
(
"Last-Event-ID"
)
if
lastEventIDHeader
!=
""
{
req
.
Header
.
Set
(
"Last-Event-ID"
,
lastEventIDHeader
)
}
client
:=
&
http
.
Client
{}
client
:=
&
http
.
Client
{}
resp
,
err
:=
client
.
Do
(
req
)
resp
,
err
:=
client
.
Do
(
req
)
...
@@ -54,17 +47,57 @@ func Relay(c *gin.Context) {
...
@@ -54,17 +47,57 @@ func Relay(c *gin.Context) {
})
})
return
return
}
}
defer
resp
.
Body
.
Close
()
scanner
:=
bufio
.
NewScanner
(
resp
.
Body
)
scanner
.
Split
(
func
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
if
atEOF
&&
len
(
data
)
==
0
{
return
0
,
nil
,
nil
}
if
i
:=
strings
.
Index
(
string
(
data
),
"
\n\n
"
);
i
>=
0
{
return
i
+
2
,
data
[
0
:
i
],
nil
}
if
atEOF
{
return
len
(
data
),
data
,
nil
}
return
0
,
nil
,
nil
})
dataChan
:=
make
(
chan
string
)
stopChan
:=
make
(
chan
bool
)
go
func
()
{
for
scanner
.
Scan
()
{
data
:=
scanner
.
Text
()
dataChan
<-
data
}
stopChan
<-
true
}()
for
k
,
v
:=
range
resp
.
Header
{
for
k
,
v
:=
range
resp
.
Header
{
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
}
}
_
,
err
=
io
.
Copy
(
c
.
Writer
,
resp
.
Body
)
c
.
Stream
(
func
(
w
io
.
Writer
)
bool
{
if
err
!=
nil
{
select
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
case
data
:=
<-
dataChan
:
"error"
:
gin
.
H
{
//fmt.Println(data)
"message"
:
err
.
Error
(),
//c.Data(http.StatusOK, "text/event-stream", []byte(data))
"type"
:
"one_api_error"
,
//c.Render(-1, common.Event{Data: data})
},
//c.SSEvent("", data)
//w.Write([]byte(data))
//w.(http.Flusher).Flush()
//c.Writer.Write(append([]byte(data), []byte("\n\n")...))
outputBytes
:=
bytes
.
NewBufferString
(
data
)
w
.
Write
(
outputBytes
.
Bytes
())
if
strings
.
HasPrefix
(
data
,
"data: "
)
{
w
.
Write
([]
byte
(
"
\n\n
"
))
}
//w.Write(append(outputBytes.Bytes(), []byte("\n\n")...))
w
.
(
http
.
Flusher
)
.
Flush
()
//fmt.Println(data)
return
true
case
<-
stopChan
:
return
false
}
})
})
return
return
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment