| 1 | /* |
| 2 | * Copyright (C) 2016 Igalia S.L. |
| 3 | * Copyright (C) 2017 Endless Mobile, Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "Cookie.h" |
| 29 | |
| 30 | #include <libsoup/soup.h> |
| 31 | #include <wtf/DateMath.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | Cookie::Cookie(SoupCookie* cookie) |
| 36 | : name(String::fromUTF8(cookie->name)) |
| 37 | , value(String::fromUTF8(cookie->value)) |
| 38 | , domain(String::fromUTF8(cookie->domain)) |
| 39 | , path(String::fromUTF8(cookie->path)) |
| 40 | , expires(cookie->expires ? static_cast<double>(soup_date_to_time_t(cookie->expires)) * 1000 : 0) |
| 41 | , httpOnly(cookie->http_only) |
| 42 | , secure(cookie->secure) |
| 43 | , session(!cookie->expires) |
| 44 | |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | static SoupDate* msToSoupDate(double ms) |
| 49 | { |
| 50 | int year = msToYear(ms); |
| 51 | int dayOfYear = dayInYear(ms, year); |
| 52 | bool leapYear = isLeapYear(year); |
| 53 | |
| 54 | // monthFromDayInYear() returns a value in the [0,11] range, while soup_date_new() expects |
| 55 | // a value in the [1,12] range, meaning we have to manually adjust the month value. |
| 56 | return soup_date_new(year, monthFromDayInYear(dayOfYear, leapYear) + 1, dayInMonthFromDayInYear(dayOfYear, leapYear), msToHours(ms), msToMinutes(ms), static_cast<int>(ms / 1000) % 60); |
| 57 | } |
| 58 | |
| 59 | SoupCookie* Cookie::toSoupCookie() const |
| 60 | { |
| 61 | if (name.isNull() || value.isNull() || domain.isNull() || path.isNull()) |
| 62 | return nullptr; |
| 63 | |
| 64 | SoupCookie* soupCookie = soup_cookie_new(name.utf8().data(), value.utf8().data(), |
| 65 | domain.utf8().data(), path.utf8().data(), -1); |
| 66 | |
| 67 | soup_cookie_set_http_only(soupCookie, httpOnly); |
| 68 | soup_cookie_set_secure(soupCookie, secure); |
| 69 | |
| 70 | if (!session) { |
| 71 | SoupDate* date = msToSoupDate(expires); |
| 72 | soup_cookie_set_expires(soupCookie, date); |
| 73 | soup_date_free(date); |
| 74 | } |
| 75 | |
| 76 | return soupCookie; |
| 77 | } |
| 78 | |
| 79 | } // namespace WebCore |
| 80 | |