前兩天零零碎碎看完了golang的基礎(chǔ),想著找個(gè)小項(xiàng)目練練手,可是出現(xiàn)了一個(gè)十分棘手的問題
我要做的東西是網(wǎng)站路徑爆破
所以我會從文本字典中把一行行路徑讀取然后與域名拼接,但是我在跑起程序后出現(xiàn)了問題
下面是一個(gè)小片段
400 Bad Request-----http://www.xxx.com/channel.asp
400 Bad Request-----http://www.xxx.com/index.asp
404 Not Found-----http://www.xxx.com/admin.asp
程序本身并沒有錯(cuò)誤,但是運(yùn)行結(jié)果就比較怪了
Bad Request?
這并不是我要說的重點(diǎn),我發(fā)現(xiàn)的問題是,除了最后一個(gè)地址,前面所有的地址都會顯示位400 Bad Request
經(jīng)過幾輪測試,我覺得應(yīng)該是網(wǎng)址拼接上出了問題
我的拼接函數(shù)是這樣
func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {
allURLTxt := bufio.NewReader(fURL)
for {
urlpath, readerError := allURLTxt.ReadString('\n')
newurl := baseurl + strings.Replace(urlpath, "\n", "", -1)
urlList = append(urlList, newurl)
if readerError == io.EOF {
fmt.Printf("\n讀取字典完成,準(zhǔn)備開始,請等待...\n")
return urlList
}
}
}
我把取一行的方式換成bufio.NewScanner就正常了
func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {
allURLTxt := bufio.NewScanner(fURL)
for allURLTxt.Scan() {
newurl := baseurl + allURLTxt.Text()
urlList = append(urlList, newurl)
}
fmt.Printf("\n讀取字典完成,準(zhǔn)備開始,請等待...\n")
return urlList
}
網(wǎng)上讀取文件一行很多人寫的文章是第一種方法,但是我也不知道什么問題導(dǎo)致這種情況的發(fā)生
我特地去查了查api文檔
func NewReader(rd io.Reader) *Reader
//NewReader returns a new Reader whose buffer has the default size.
func (b *Reader) ReadString(delim byte) (string, error)
//ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim. For simple uses, a Scanner may be more convenient.
func NewScanner(r io.Reader) *Scanner
//NewScanner returns a new Scanner to read from r. The split function defaults to ScanLines.
func (s *Scanner) Scan() bool
//Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil. Scan panics if the split function returns 100 empty tokens without advancing the input. This is a common error mode for scanners.
func (s *Scanner) Text() string
//Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.
按照上面的api文檔,這兩個(gè)的區(qū)別就是兩者在返回string的時(shí)候,一個(gè)是數(shù)據(jù)+分隔符,一個(gè)是一行的數(shù)據(jù),不帶分隔符
雖說我第一種方法也用strings.Replace方法把"\n"替換成了""空字符,但是可能還是有點(diǎn)奇奇怪怪的東西