2018년 2월 1일 목요일

엑셀 url decoding

웹브라우저 주소창에서 볼 수 있는 URL 문자열에는 공백문자가 들어갈 수 없다.



URL에 공백문자가 포함되면 URL 구조가 깨지기 때문.


The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.

The characters "<" and ">" are unsafe because they are used as the delimiters around URLs in free text; the quote mark (""") is used to delimit URLs in some systems.

The character "#" is unsafe and should always be encoded because it is used in World Wide Web and in other systems to delimit a URL from a fragment/anchor identifier that might follow it.

The character "%" is unsafe because it is used for encodings of other characters. Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters.

These characters are "{", "}", "|", "\", "^", "~",  "[", "]", and "`".

그래서 공백문자를 포함한 몇몇 특수기호들은 전송 과정에서 아스키코드 체계의 16진수로 변환되며, 이때 '%20'식으로 %를 붙여서 순수 문자와 구분한다. 문제는 이런 문자열은 (컴퓨터가 아닌 사람이) 읽기가 아주 고약하다는 사실.

URL은 웹서버로 데이터를 전송하는 주요 수단이기 때문에 보안 관점에서도 들여다 볼 일이 많은데 URL 인코딩 때문에 읽기가 쉽지 않다.


그래서 엑셀을 이용해서 웹로그를 분석할 때면 치환 기능을 이용해서 인코딩 문자를 원래 문자로 변환하곤 하는데 이게 또 은근 귀찮음.


아예 URL 인코딩 문자를 디코딩할 수 있는 VBA 함수를 만들어보자. 'ALT + F11'키를 눌러서 VBA 편집기 실행 후 모듈 삽입.


DECODEURL 함수 코드 입력.
Function DECODEURL(varText As Variant, Optional blnEncode = True)
  Static objHtmlfile As Object

  If objHtmlfile Is Nothing Then
    Set objHtmlfile = CreateObject("htmlfile")
    With objHtmlfile.parentWindow
      .execScript "function decode(s) {return decodeURIComponent(s)}", "jscript"
    End With
  End If

  If blnEncode Then
    DECODEURL = objHtmlfile.parentWindow.decode(varText)
  End If
End Function


입력과 동시에 함수가 만들어진다.


디코딩도 잘 됨.


추가한 함수는 해당 함수를 만든 문서에서만 사용 가능한데, 파일로 내보내면 다른 문서에서 불러와 사용할 수 있다.



이런 귀찮은 작업이 싫다면 웹로그 저장할 때, 아예 처음부터 웹로그를 디코딩해서 저장하면 된다. 로그스태시 urldecode 필터 추천.

관련 글

댓글 없음:

댓글 쓰기

크리에이티브 커먼즈 라이선스