dongsheng8158 2018-07-05 17:06
浏览 88

Cookie返回有效期错误

I have a search results page, but when I try and go to the next page for more results, I am getting an error. I suspect that it is because a cookie is not being encrypted before it is decoded. However, I have a limited knowledge of cookies and am not 100% sure.

This is the handler for the results page:

    // Handles displaying results of queries to item database
func handleSearch(w http.ResponseWriter, req *http.Request) {
    t := templating.Templates.Lookup("results.html")
    if t == nil {
        log.Printf("Cannot find results.html in template
")
    }
    user := auth.Authorize(w, req)
    var c []*structs.Entry
    if user != nil {
        c, _ = cart.ReadCart(req, user)
    } else {
        c = nil
    }

    // Set up page data struct
    data := structs.PageData{User: user, PageID: structs.SearchResults,
        Constants: structs.ConstMap, Cart: c}

    // Do presearch stuff
    var results []*structs.Item
    currPage, status, results, err := parsePreSearch(req)
    if err != nil {
        log.Printf("presearch error: %v", err)
    }

    // Get search results
    if req.PostFormValue("type") == "qck" {
        data.SearchParams = req.URL.Query().Get("search")
        results, err = parseAndQuickSearch(req, status, &data, user)
        if err != nil {
            log.Printf("quick search error: %v", err)
        }
    } else if req.PostFormValue("type") == "adv" {
        results, err = parseAndAdvancedSearch(req, &data, user)
        if err != nil {
            log.Printf("advanced search error: %v", err)
        }
        data.Errors = append(data.Errors, structs.NoInactive)
    }
    search.StartResults(w, req, results, structs.SearchResults, status)

    // Now that search is complete, get and assign more page data
    maxPage := int(math.Ceil(float64(len(results)) / 8))
    if !(currPage <= maxPage && currPage >= 0) && (maxPage != 0) { //if page is not within range
        log.Printf("Page out of bounds, 404 Error at URL %s", req.URL.String())
        handleNotFound(w, req)
        return
    }

    data.SearchResults = results
    data.MaxPage = maxPage
    data.PageNum = currPage
    if len(results) == 0 {
        data.Errors = append(data.Errors, structs.NoResults)
    }
    utils.SetMimeType(w, "results.html")

    // Serve the page
    err = t.Execute(w, data)
    if err != nil {
        log.Printf("Error delivering search.html with err: %v
", err)
        return
    }
}

The parsePreSearch is what has been giving me trouble.

// Function for parsing multiple pre-search arguments
// Returns current page, status of search, list of items, and an error
func parsePreSearch(req *http.Request) (int, int, []*structs.Item, error) {
    // Get page number
    err := req.ParseForm()
    var currPage int
    pagequery := req.PostFormValue("curr_page")
    if pagequery == "" {
        pagequery = "1"
    }
    changepage := req.PostFormValue("prev_next")
    if pagequery == "all" || changepage == "all" {
        currPage = 0
    } else {
        currPage, err = strconv.Atoi(pagequery)
        if err != nil {
            return -1, -1, nil, err
        }
        if changepage == "next" {
            currPage = currPage + 1
        } else if changepage == "prev" {
            if currPage > 1 {
                currPage = currPage - 1
            }
        }
    }

    // Read search results cookie, erroring if the cookie errors

    bp, err := search.ReadBackPage(req)
    if err != nil {
        return currPage, structs.Active, nil, err
    }

    var results []*structs.Item
    // Determine what items should not be shown
    showstatus := bp.PageStatus //what status will be shown
    if req.PostFormValue("inactive") == "show" {
        showstatus = structs.ViewAll
    } else if req.PostFormValue("inactive") == "hide" {
        showstatus = structs.Active
    }

    // Read stored search results if necessary
    if req.PostFormValue("back") == "y" {
        if bp != nil { // If there is a cookie
            results = search.GetResults(bp)
            return currPage, showstatus, results, nil
        } else {
            return currPage, -1, nil, err
        }
    } else {
        return currPage, showstatus, nil, nil
    }
}

And finally, I suspect that ReadBackPage is what's giving me the problem. I am getting the error: securecookie: cookie is not valid.

// Read the previous page
func ReadBackPage(req *http.Request) (*structs.BackPage, error) {
    var chip = securecookie.New(hashkey, blockkey)
    var bp structs.BackPage
    results, err := req.Cookie("_results")
    if err != nil {
        return nil, err
    }
    err = chip.Decode("_results", results.Value, &bp)
    log.Printf("Decoded results: %v", bp)
    if err != nil {
        return nil, err
    }
    return &bp, nil
}

I am fixing someone else's code, so I am not exactly sure what the exact functionality of ReadBackPage was, but it is failing when called. Any ideas why?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用