aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Iis/ca/scamimemap.cpp
blob: 8afe99f95f718fd4e8f1b8f5b66952aa8aad7508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

enum eMimeMapQuery { mmqMimeMap = 1, mmqParentType, mmqParentValue, 
                        mmqMimeType, mmqExtension};

// prototypes
static HRESULT AddMimeMapToList(SCA_MIMEMAP** ppsmmList);


void ScaMimeMapFreeList(SCA_MIMEMAP* psmmList)
{
    SCA_MIMEMAP* psmmDelete = psmmList;
    while (psmmList)
    {
        psmmDelete = psmmList;
        psmmList = psmmList->psmmNext;

        MemFree(psmmDelete);
    }
}


HRESULT __stdcall ScaMimeMapRead(
    SCA_MIMEMAP** ppsmmList,
    __inout LPWSTR *ppwzCustomActionData
    )
{
    HRESULT hr = S_OK;
    MSIHANDLE hRec;
    LPWSTR pwzData = NULL;
    SCA_MIMEMAP* psmm;
    WCA_WRAPQUERY_HANDLE hWrapQuery = NULL;

    hr = WcaBeginUnwrapQuery(&hWrapQuery, ppwzCustomActionData);
    ExitOnFailure(hr, "Failed to unwrap query for ScaMimeMapRead");

    if (0 == WcaGetQueryRecords(hWrapQuery))
    {
        WcaLog(LOGMSG_VERBOSE, "Skipping ScaMimeMapRead() - required table not present");
        ExitFunction1(hr = S_FALSE);
    }

    // loop through all the mimemappings
    while (S_OK == (hr = WcaFetchWrappedRecord(hWrapQuery, &hRec)))
    {
        hr = AddMimeMapToList(ppsmmList);
        ExitOnFailure(hr, "failed to add mime map to list");

        psmm = *ppsmmList;

        hr = WcaGetRecordString(hRec, mmqMimeMap, &pwzData);
        ExitOnFailure(hr, "Failed to get MimeMap.MimeMap");
        hr = ::StringCchCopyW(psmm->wzMimeMap, countof(psmm->wzMimeMap), pwzData);
        ExitOnFailure(hr, "Failed to copy mimemap string to mimemap object");

        hr = WcaGetRecordInteger(hRec, mmqParentType, &psmm->iParentType);
        ExitOnFailure(hr, "Failed to get MimeMap.iParentType");
    
        hr = WcaGetRecordString(hRec, mmqParentValue, &pwzData);
        ExitOnFailure(hr, "Failed to get MimeMap.ParentValue");
        hr = ::StringCchCopyW(psmm->wzParentValue, countof(psmm->wzParentValue), pwzData);
        ExitOnFailure(hr, "Failed to copy parent value string to mimemap object");

        hr = WcaGetRecordString(hRec, mmqExtension, &pwzData);
        ExitOnFailure(hr, "Failed to get MimeMap.Extension");
        hr = ::StringCchCopyW(psmm->wzExtension, countof(psmm->wzExtension), pwzData);
        ExitOnFailure(hr, "Failed to copy extension string to mimemap object");

        hr = WcaGetRecordString(hRec, mmqMimeType, &pwzData);
        ExitOnFailure(hr, "Failed to get MimeMap.MimeType");
        hr = ::StringCchCopyW(psmm->wzMimeType, countof(psmm->wzMimeType), pwzData);
        ExitOnFailure(hr, "Failed to copy mimetype string to mimemap object");
    }

    if (E_NOMOREITEMS == hr)
        hr = S_OK;
    ExitOnFailure(hr, "Failure while processing mimemappings");

LExit:
    WcaFinishUnwrapQuery(hWrapQuery);

    ReleaseStr(pwzData);

    return hr;
}


HRESULT ScaGetMimeMap(int iParentType, LPCWSTR wzParentValue, SCA_MIMEMAP **ppsmmList, SCA_MIMEMAP **ppsmmOut)
{
    HRESULT hr = S_OK;
    SCA_MIMEMAP* psmmAdd = NULL;
    SCA_MIMEMAP* psmmLast = NULL;

    *ppsmmOut = NULL;
    
    if (!*ppsmmList)
        return hr;

    SCA_MIMEMAP* psmm = *ppsmmList;
    while (psmm)
    {
        if (iParentType == psmm->iParentType && 0 == lstrcmpW(wzParentValue, psmm->wzParentValue))
        {
            // Found a match, take this one out of the list and add it to the matched out list
            psmmAdd = psmm;

            if (psmmLast)
            {
                // If we're not at the beginning of the list tell the last node about it's new next (since we're taking away it's current next)
                psmmLast->psmmNext = psmmAdd->psmmNext;
            }
            else
            {
                // If we are at the beginning (no psmmLast) update the beginning (since we're taking it)
                *ppsmmList = psmm->psmmNext;
            }
            psmm = psmm->psmmNext; // move on

            // Add the one we've removed to the beginning of the out list
            psmmAdd->psmmNext = *ppsmmOut;
            *ppsmmOut = psmmAdd;
        }
        else
        {
            psmmLast = psmm; // remember the last we that didn't match
            psmm = psmm->psmmNext; // move on
        }
    }

    return hr;
}

HRESULT ScaMimeMapCheckList(SCA_MIMEMAP* psmmList)
{
    if (!psmmList)
        return S_OK;
    
    while (psmmList)
    {
        WcaLog(LOGMSG_STANDARD, "MimeMapping of %ls with ParentType=%d and ParentValue=%ls not used!", psmmList->wzMimeMap, psmmList->iParentType, psmmList->wzParentValue);
        psmmList = psmmList->psmmNext;
    }

    return E_FAIL;
}


HRESULT ScaWriteMimeMap(IMSAdminBase* piMetabase, LPCWSTR wzRootOfWeb, 
                               SCA_MIMEMAP* psmmList)
{
    HRESULT hr = S_OK;

    WCHAR wzMimeMap[8192];
    WCHAR *pwzNext = wzMimeMap;
    const WCHAR *pwzMac = wzMimeMap + countof(wzMimeMap); // used to properly create the MULTI_SZ

    // fill the MULTI_SZ wzMimeMap buffer for the MimeMap attribute
    ::ZeroMemory(wzMimeMap, sizeof(wzMimeMap));

    for (SCA_MIMEMAP* psmm = psmmList; psmm; psmm = psmm->psmmNext)
    {
        hr = ::StringCchPrintfW(pwzNext, max(0, pwzMac - pwzNext), L"%s,%s", psmm->wzExtension, psmm->wzMimeType);
        ExitOnFailure(hr, "Failed to set MimeMap string");

        pwzNext += lstrlenW(pwzNext) + 1; // reserve space for null
        Assert(pwzNext <= pwzMac);
    }

    if (pwzNext != wzMimeMap)
    {
        // now write the CustomErrors to the metabase
        hr = ScaWriteMetabaseValue(piMetabase, wzRootOfWeb, NULL, MD_MIME_MAP, METADATA_INHERIT, IIS_MD_UT_FILE, MULTISZ_METADATA, wzMimeMap);
        ExitOnFailure(hr, "Failed to write MimeMap");
    }
    else
    {
        WcaLog(LOGMSG_VERBOSE, "Skipping ScaWriteMimeMap() - no mappings found.");
        ExitFunction1(hr = S_FALSE);
    }

LExit:
    return hr;
}


static HRESULT AddMimeMapToList(SCA_MIMEMAP** ppsmmList)
{
    HRESULT hr = S_OK;

    SCA_MIMEMAP* psmm = static_cast<SCA_MIMEMAP*>(MemAlloc(sizeof(SCA_MIMEMAP), TRUE));
    ExitOnNull(psmm, hr, E_OUTOFMEMORY, "failed to allocate memory for new mime map list element");

    psmm->psmmNext = *ppsmmList;
    *ppsmmList = psmm;
    
LExit:
    return hr;
}