utility.c 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* 
 * Wininet - Utility functions
 *
 * Copyright 1999 Corel Corporation
 *
 * Ulrich Czekalla
 *
 */

10 11
#include "config.h"

12 13 14 15
#include <stdlib.h>
#include <string.h>
#include <time.h>

16 17
#include "windef.h"
#include "winbase.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "wininet.h"
#include "winerror.h"

#include "debugtools.h"
#include "internet.h"

DEFAULT_DEBUG_CHANNEL(wininet);

#define TIME_STRING_LEN  30

time_t ConvertTimeString(LPCSTR asctime)
{
    char tmpChar[TIME_STRING_LEN];
    char *tmpChar2;
32
    struct tm t;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    int timelen = strlen(asctime);

    if(!asctime || !timelen)
        return 0;

    strncpy(tmpChar, asctime, TIME_STRING_LEN);

    /* Assert that the string is the expected length */
    if (tmpChar[TIME_STRING_LEN] != '\0')
    { 
        tmpChar[TIME_STRING_LEN] = '\0';
        FIXME("\n");
    }
    
    /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
     * We assume the time is in this format
     * and divide it into easy to swallow chunks
     */
    tmpChar[3]='\0';
    tmpChar[7]='\0';
    tmpChar[11]='\0';
    tmpChar[16]='\0';
    tmpChar[19]='\0';
    tmpChar[22]='\0';
    tmpChar[25]='\0';

59 60 61 62 63
    t.tm_year = atoi(tmpChar+12) - 1900;
    t.tm_mday = atoi(tmpChar+5);
    t.tm_hour = atoi(tmpChar+17);
    t.tm_min = atoi(tmpChar+20);
    t.tm_sec = atoi(tmpChar+23);
64 65 66 67 68 69 70
	
    /* and month */
    tmpChar2 = tmpChar + 8;
    switch(tmpChar2[2])
    {
        case 'n':
            if(tmpChar2[1]=='a')
71
                t.tm_mon = 0;
72
            else
73
                t.tm_mon = 5;
74 75
            break;
        case 'b':
76
            t.tm_mon = 1;
77 78 79
            break;
        case 'r':
            if(tmpChar2[1]=='a')
80
                t.tm_mon = 2;
81
            else
82
                t.tm_mon = 3;
83 84
            break;
        case 'y':
85
            t.tm_mon = 4;
86 87
            break;
        case 'l':
88
            t.tm_mon = 6;
89 90
            break;
        case 'g':
91
            t.tm_mon = 7;
92 93
            break;
        case 'p':
94
            t.tm_mon = 8;
95 96
            break;
        case 't':
97
            t.tm_mon = 9;
98 99
            break;
        case 'v':
100
            t.tm_mon = 10;
101 102
            break;
        case 'c':
103
            t.tm_mon = 11;
104 105 106 107 108
            break;
        default:
            FIXME("\n");
    }

109
    return mktime(&t);
110 111 112 113 114 115
}


BOOL GetAddress(LPCSTR lpszServerName, INTERNET_PORT nServerPort,
	struct hostent **phe, struct sockaddr_in *psa)
{
116 117
    char *found;

118 119
    TRACE("%s\n", lpszServerName);

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /* Validate server name first
     * Check if there is sth. like
     * pinger.macromedia.com:80
     * if yes, eliminate the :80....
     */
    found = strchr(lpszServerName, ':');
    if (found)
    {
        int len = found - lpszServerName;
        char *new = HeapAlloc(GetProcessHeap(), 0, len + 1);
        memcpy( new, lpszServerName, len );
        new[len] = '\0';
        TRACE("Found a ':' inside the server name, reparsed name: %s\n", new);
        *phe = gethostbyname(new);
        HeapFree( GetProcessHeap(), 0, new );
    }
    else *phe = gethostbyname(lpszServerName);

138 139
    if (NULL == *phe)
    {
140
        TRACE("Failed to get hostname: (%s)\n", lpszServerName);
141 142 143 144 145 146 147 148 149 150
        return FALSE;
    }

    memset(psa,0,sizeof(struct sockaddr_in));
    memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
    psa->sin_family = (*phe)->h_addrtype;
    psa->sin_port = htons((u_short)nServerPort);

    return TRUE;
}