Prechádzať zdrojové kódy

issue #87 - fully parse profile HTML

Mike 1 rok pred
rodič
commit
b0d6e85287
2 zmenil súbory, kde vykonal 49 pridanie a 18 odobranie
  1. 14 16
      server/toc/http.go
  2. 35 2
      server/toc/http_test.go

+ 14 - 16
server/toc/http.go

@@ -163,10 +163,10 @@ func (s OSCARProxy) ProfileHandler(w http.ResponseWriter, r *http.Request) {
 
 		pd := struct {
 			ScreenName string
-			Profile    string
+			Profile    template.HTML
 		}{
 			ScreenName: user,
-			Profile:    extractBodyContent(profile),
+			Profile:    template.HTML(extractProfile(profile)),
 		}
 
 		if err := profileTemplate.Execute(w, pd); err != nil {
@@ -332,11 +332,13 @@ func (s OSCARProxy) logAndReturn500(ctx context.Context, w http.ResponseWriter,
 	http.Error(w, "internal server error", http.StatusInternalServerError)
 }
 
-// extractBodyContent parses an HTML string and extracts the content within <BODY>...</BODY> tags.
-func extractBodyContent(htmlContent []byte) string {
+// extractProfile extracts the contents of an HTML <BODY>. If there's no HTML
+// body, just return the text.
+//
+// It only returns the following HTML tags: <b> <i> <font> <a> <u> <br>
+func extractProfile(htmlContent []byte) string {
 	tokenizer := html.NewTokenizer(bytes.NewReader(htmlContent))
 	var bodyContent bytes.Buffer
-	inBody := false
 
 	for {
 		switch tokenizer.Next() {
@@ -345,20 +347,16 @@ func extractBodyContent(htmlContent []byte) string {
 				return "unable to read profile"
 			}
 			return bodyContent.String()
-		case html.StartTagToken:
-			token := tokenizer.Token()
-			if token.Data == "body" {
-				inBody = true
-			}
-		case html.EndTagToken:
+		case html.StartTagToken, html.EndTagToken:
 			token := tokenizer.Token()
-			if token.Data == "body" {
-				inBody = false
+			switch token.Data {
+			case "b", "i", "font", "a", "u", "br":
+				bodyContent.WriteString(token.String())
 			}
 		case html.TextToken:
-			if inBody {
-				bodyContent.Write(tokenizer.Text())
-			}
+			bodyContent.Write(tokenizer.Text())
 		}
 	}
+
+	return bodyContent.String()
 }

+ 35 - 2
server/toc/http_test.go

@@ -40,7 +40,40 @@ func TestOSCARProxy_NewServeMux(t *testing.T) {
 		mockParams mockParams
 	}{
 		{
-			name:           "Successfully retrieve profile",
+			name:           "Successfully retrieve HTML profile",
+			path:           "/info?from=me&user=them&cookie=" + cookie,
+			expectedStatus: http.StatusOK,
+			expectedBody:   `<font lang="0"><a href="aim:GoChat?RoomName=General&amp;Exchange=4">Let's chat</font></a><br><br><font color="#ff0000" lang="0">colorfg</font><font color="#000000"> </font><font back="#00ff00">colorbg</font><font> </font><font size="4">big</font><font size="3"> <b></font><font>bold</b></font><font> <i></font><font>italic</i></font><font> <u></font><font>underline</u></font><font> 8-)</font>`,
+			mockParams: mockParams{
+				locateParams: locateParams{
+					userInfoQueryParams: userInfoQueryParams{
+						{
+							me: state.NewIdentScreenName("me"),
+							inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
+								Type:       uint16(wire.LocateTypeSig),
+								ScreenName: "them",
+							},
+							msg: wire.SNACMessage{
+								Frame: wire.SNACFrame{
+									FoodGroup: wire.Locate,
+									SubGroup:  wire.LocateUserInfoReply,
+								},
+								Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
+									TLVUserInfo: newTestSession("them").TLVUserInfo(),
+									LocateInfo: wire.TLVRestBlock{
+										TLVList: wire.TLVList{
+											wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, `"<HTML><BODY BGCOLOR="#ffffff"><FONT LANG="0"><A HREF="aim:GoChat?RoomName=General&Exchange=4">Let's chat</FONT></A><BR><BR><FONT COLOR="#ff0000" LANG="0">colorfg</FONT><FONT COLOR="#000000"> </FONT><FONT BACK="#00ff00">colorbg</FONT><FONT> </FONT><FONT SIZE=4>big</FONT><FONT SIZE=3> <B></FONT><FONT>bold</B></FONT><FONT> <I></FONT><FONT>italic</I></FONT><FONT> <U></FONT><FONT>underline</U></FONT><FONT> 8-)</FONT></BODY></HTML>"`),
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+		{
+			name:           "Successfully retrieve plaintext profile",
 			path:           "/info?from=me&user=them&cookie=" + cookie,
 			expectedStatus: http.StatusOK,
 			expectedBody:   "My profile!",
@@ -62,7 +95,7 @@ func TestOSCARProxy_NewServeMux(t *testing.T) {
 									TLVUserInfo: newTestSession("them").TLVUserInfo(),
 									LocateInfo: wire.TLVRestBlock{
 										TLVList: wire.TLVList{
-											wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "<HTML><BODY>My profile!</BODY></HTML>"),
+											wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "My profile!"),
 										},
 									},
 								},