Cookie返回有效期错误

时间:2018-07-05 17:06:01

标签: go cookies session-cookies

我有一个搜索结果页面,但是当我尝试转到下一页以获取更多结果时,出现错误。我怀疑这是因为cookie在解码之前没有被加密。但是,我对Cookie的了解有限,并且不确定100%。

这是结果页面的处理程序:

    // 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\n")
    }
    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\n", err)
        return
    }
}

parsePreSearch一直给我带来麻烦。

// 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
    }
}

最后,我怀疑ReadBackPage是导致我遇到问题的原因。我收到错误消息:securecookie:cookie无效。

// 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
}

我正在修复其他人的代码,因此我不能完全确定ReadBackPage的确切功能是什么,但是调用时失败。有什么想法吗?

0 个答案:

没有答案