{"id":13777434,"url":"https://github.com/spacewander/lua-resty-mime-sniff","last_synced_at":"2026-02-28T05:53:24.595Z","repository":{"id":71091886,"uuid":"98001446","full_name":"spacewander/lua-resty-mime-sniff","owner":"spacewander","description":"Sniff the real MIME type of given data in your OpenResty app","archived":false,"fork":false,"pushed_at":"2018-04-05T11:08:51.000Z","size":11,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T03:15:42.199Z","etag":null,"topics":["openresty","security","websecurity"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spacewander.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-07-22T02:31:32.000Z","updated_at":"2024-11-15T18:06:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"c8f16763-186e-4ac4-a372-62ccd40d4e12","html_url":"https://github.com/spacewander/lua-resty-mime-sniff","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/spacewander/lua-resty-mime-sniff","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-mime-sniff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-mime-sniff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-mime-sniff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-mime-sniff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spacewander","download_url":"https://codeload.github.com/spacewander/lua-resty-mime-sniff/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-mime-sniff/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29925847,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"online","status_checked_at":"2026-02-28T02:00:07.010Z","response_time":90,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["openresty","security","websecurity"],"created_at":"2024-08-03T18:00:43.316Z","updated_at":"2026-02-28T05:53:24.578Z","avatar_url":"https://github.com/spacewander.png","language":"Lua","funding_links":[],"categories":["\u003ca id=\"7bf0f5839fb2827fdc1b93ae6ac7f53d\"\u003e\u003c/a\u003e工具","Libraries"],"sub_categories":["\u003ca id=\"32739127f0c38d61b14448c66a797098\"\u003e\u003c/a\u003e嗅探\u0026\u0026Sniff"],"readme":"## Name\n\nlua-resty-mime-sniff - Sniff the real MIME type of given data.\n\n## Status\n\n[![Travis](https://travis-ci.org/spacewander/lua-resty-mime-sniff.svg?branch=master)](https://travis-ci.org/spacewander/lua-resty-mime-sniff)\n[![Coverage Status](https://coveralls.io/repos/github/spacewander/lua-resty-mime-sniff/badge.svg?branch=master)](https://coveralls.io/github/spacewander/lua-resty-mime-sniff?branch=master)\n\n## Synopsis\n\n```lua\n-- ￥ curl -F \"file=@evil_script.jpg;type=image/jpeg\"  localhost:8888\n-- You saied 'image/jpeg', but we found 'text/plain'\nlocal mime_sniff = require \"lib.mime_sniff\"\nlocal upload = require \"resty.upload\"\n\nlocal chunk_size = 1445 -- 512 is enough for the most cases\n\nlocal form, err = upload:new(chunk_size)\nif not form then\n    ngx.log(ngx.ERR, \"failed to new upload: \", err)\n    ngx.exit(500)\nend\n\nform:set_timeout(1000)\n\nlocal submit_content_type\nlocal actual_content_type\nwhile true do\n    local typ, res, err = form:read()\n    if not typ then\n        ngx.say(\"failed to read: \", err)\n        return\n    end\n\n    if typ == \"header\" and res[1] == \"Content-Type\" then\n        submit_content_type = res[2]\n    elseif typ == \"body\" then\n        --if not mime_sniff.match_content_type(\"image/jpeg\") then\n        --if not mime_sniff.match_content_type({\"image/gif\", \"image/jpeg\"}) then\n        actual_content_type = mime_sniff.detect_content_type(res)\n        break\n    elseif typ == \"eof\" then\n        break\n    end\nend\nngx.say(\"You saied '\", submit_content_type, \"', but we found '\", actual_content_type, \"'\")\n```\n\n## Methods\n\ndetect_content_type\n---\n`syntax: content_type = mime_sniff.detect_content_type(data)`\n\n`detect_content_type` could be used to detect real Content-Type of the given data.\nIt considers at most the first 1445 bytes of data,\nthough first 512 bytes are enough for the majority of cases.\nThis function always returns a valid MIME type:\nif it cannot determine a more specific one, it returns \"application/octet-stream\".\n\n\nmatch_content_type\n---\n`syntax: match_content_type = mime_sniff.match_content_type(data, types, ...)`\n\n`match_content_type` checks if the given data's Content-Type matches any of the given types.\nThe types is an array-like table or strings, and the length of data should be long enough.\n(1445 bytes at most and 512 bytes is enough)\nNote that the order of types is important. The first type matches first.\nThis function will return matched type or nil,\nor throw an error if none of the given types is supported yet.\nFor the list of supported mime types, please refer to the wiki:\nhttps://github.com/spacewander/lua-resty-mime-sniff/wiki/MIME-type-support-status\n\n## Installation\n\nYou could install via `opm --cwd get spacewander/lua-resty-mime-sniff`.(Recommended)\nOr you could vendor the `lib/` in your project.\n\n## Contributing\n\nAny contribution are welcome! However, before writing your pull request, please read through [the Guide](CONTRIBUTING.md).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Flua-resty-mime-sniff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspacewander%2Flua-resty-mime-sniff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Flua-resty-mime-sniff/lists"}